As the lead of a motor-controller firmware or a console game engine in C++, how do you decide whether to disable exceptions and RTTI, and what must the team standardise if you do?
answer
- size, timing and certification
- third-party code assumes throwing
- one error type, decided early
- what aborts and what recovers
- enforce it in the build
basics
~20 sDisable them when image size, worst-case timing or certification evidence outweigh the cost of hand-written error paths and of libraries that assume exceptions; then standardise one error type, the fatal-error handler, the banned library calls and the enforcement.
solid answer
~50 sThe case for turning exceptions off is image size (unwind tables and landing pads), timing that must be analysable, and certification evidence for every control path; the case for RTTI off is the per-class type data, and both remain compiler dialects, not ISO C++. The cost is real: every failure becomes a hand-written return path, constructors need factories, and third-party code that throws must be wrapped, patched or excluded. If you go ahead, decide once for the whole codebase: a single error vocabulary (an `enum class`, `std::error_code` or C++23 `std::expected`), what a library precondition failure does (it will abort, so own the abort handler), which library calls are banned (`at`, `value()`, `std::stoi`, plain `new`), what replaces `dynamic_cast` (an engine type id or `virtual` queries), and how CI enforces it, including that plugins match the setting.
go deeper
Know the usual reasons for turning off exceptions and RTTI: image size, predictable timing and certification evidence.
Explain what the team gives up: hand-written error paths, factory constructors and library calls that abort.
Plan the third-party boundary, the fatal-error handler and how CI enforces the flags across every target.
Weigh measured size and timing gains against the cost of the dialect, decide where the line falls, and fix one error vocabulary early.
## Two codebases, two different questions The decision looks the same in a motor-controller firmware and a console game engine, but the forces differ. - **Firmware** cares about flash size, worst-case execution time and the evidence a safety assessor wants. A throw's cost is hard to bound, and an assessor will ask for every path through the handler tables. Exceptions off also removes the runtime's exception-object allocation, which matters when the heap is banned. - **A game engine** cares about binary size on fixed-memory hardware, predictable frame time, and a codebase of many millions of lines where a hidden control-flow path is expensive to reason about. Engines also tend to have their own reflection system, which makes RTTI redundant. Neither is a standards question: the ISO standard has no subset without exceptions or RTTI. You are choosing a compiler dialect, and every tool, library and new hire has to accept it. ## What you buy 1. **Size.** GCC's manual says that on some targets exception support makes the compiler emit frame unwind information for all functions, which "can produce significant data size overhead". The libstdc++ manual cites around 7% of combined code and data for one dated configuration, and notes that `-Os` with exceptions enabled can save more. Measure your own image instead of quoting either number. 2. **Analysable control flow.** Every exit from a function is visible in its source, which helps static analysis, timing analysis and code review. 3. **Less type data with RTTI off.** GCC documents the saving as the type information it no longer generates for every class with virtual functions. ## What you pay - **Error paths by hand.** Every fallible call returns an error that every caller must check; constructors become factories; operators and destructors must be failure-free. - **Library behaviour you do not control.** `std::vector::at`, `std::expected::value()` and plain `new` cannot throw, so each library ends the program instead, by its own route. Some members also change without RTTI (libstdc++'s `std::get_deleter` silently returns null). - **Third-party code.** A library that uses exceptions for errors must be wrapped behind a boundary built with exceptions, patched, or rejected. Mixing translation units built with and without RTTI can fail to link. - **Hiring and tooling.** Developers and examples assume standard C++; your guide has to say what differs. ## What to standardise if you go ahead | Decision | Options | Usual choice | |---|---|---| | Error vocabulary | `enum class` codes, `std::error_code`, `std::expected` (C++23), an in-house result type | one type per layer, chosen before the code grows | | Precondition failure in the library | accept the library's abort, or install a handler | own the fatal path: log, put the hardware in a safe state, reset | | Allocation failure | `new (std::nothrow)`, replaced `operator new`, no heap | firmware: no heap after start-up; engine: custom allocators | | Banned calls | `at`, `value()`, `std::stoi`, `std::function::target` | a lint rule, not a wiki page | | Replacement for `dynamic_cast` | a virtual `kind()` query, an engine type id, `std::variant` | whatever the engine's reflection already provides | | Third-party boundary | wrap, patch, or exclude | a wrapper library built with exceptions that converts them to error values at its edge | ## Coding standards and enforcement Safety-oriented guidelines such as MISRA C++ define a restricted subset with checkable rules and ask for any departure to be recorded as a documented deviation. The point for a lead is the same either way: write the subset down, give it a checker, and make CI fail on a violation. The build should set the flags for every target in one place, test the `__cpp_exceptions` and `__cpp_rtti` macros in shared headers, and refuse a plugin or third-party binary built with a different setting. ## A staged rollout Switching an existing codebase is a project, not a flag change: 1. Build a branch with both settings off and collect every compile error; each is a call site to redesign. 2. Grep for members that change silently, such as `std::get_deleter`, and for library calls that will now abort. 3. Introduce the error vocabulary and the factory pattern in the leaf modules first, then move inward. 4. Measure the image size and the worst-case timings before and after, and keep the numbers as the record of why the decision was taken. ## When the answer is no A host-side tool, a test harness or a server component rarely gains enough to justify the dialect. Many teams split the line: exceptions and RTTI off in the device image or the engine runtime, on in the editor, the asset pipeline and the tests, with an error-value boundary between the two.
- How do you use a third-party C++ library that reports errors by throwing in a codebase built without exceptions?Put it behind a thin wrapper library compiled with exceptions enabled. The wrapper catches everything at its boundary and converts it to the codebase's error type, so no exception ever crosses into code built without unwind support. If that is impossible on the target, patch the library or choose another.
- What replaces dynamic_cast in an engine built without RTTI?Something the engine controls: a virtual `kind()` query or a type id stored in each object and compared before a `static_cast`, a `std::variant` when the set of types is closed, or the engine's own reflection data. The choice should be one convention, not each team's own.
It is like a building banning extension cords: every appliance needs its own wall socket planned in advance, but nobody ever trips over a cable during an inspection.
- Disabling exceptions makes all error handling free.
- The ISO standard defines a safe subset without exceptions or RTTI.
- Third-party libraries work unchanged when exceptions are disabled.
- Turning off exceptions guarantees a fixed percentage size reduction.
- Each module can pick its own error type and conversion later.
Assumes C++23 for std::expected as the error vocabulary; the other options work from C++11 or C++17.