In a C++ game engine built with RTTI disabled, which standard library members stop compiling or quietly change behaviour, and do libc++ and libstdc++ agree?
answer
- typeid behind library members
- any::type and function::target
- get_deleter needs a type id
- removed in one, null in the other
- exceptions keep their type info
basics
~10 sMembers that answer with a std::type_info vanish: std::any::type and std::function::target_type. libc++ also drops std::function::target and std::get_deleter, while libstdc++ keeps them and get_deleter silently returns null. std::any_cast keeps working.
solid answer
~30 sWithout RTTI the compiler rejects `typeid` and `dynamic_cast` to a derived class, so library members built on them change. Both libraries remove the members that return a `std::type_info`: `std::any::type()` and `std::function::target_type()`. They part ways on the rest. **libc++** also removes `std::function::target` and `std::get_deleter` for `shared_ptr`, so code using them fails to compile. **libstdc++** keeps both: `target` compares internal manager-function addresses, and `get_deleter` compiles but always returns a null pointer, which is a silent behaviour change. `std::any_cast` works in both, comparing an internal per-type address instead of `typeid`. Exceptions still work, because the compiler emits type information for thrown types as needed.
code
plaintext · 5 linesrtti.cpp:14:40: error: no member named 'target' in 'std::function<int (int)>'
rtti.cpp:18:39: error: no member named 'get_deleter' in namespace 'std'; did you mean 'std::__shared_weak_count::__get_deleter'?
rtti.cpp:21:25: error: no member named 'type' in 'std::any'
tid.cpp:8:23: error: use of typeid requires -frtti
tid.cpp:11:30: error: use of dynamic_cast requires -frttigo deeper
Recall that typeid and checked dynamic_cast stop compiling, while exceptions still catch by type.
Name the library members built on std::type_info and explain how any_cast survives by comparing internal per-type addresses.
Show the silent libstdc++ get_deleter change, how you would find such call sites, and why plugins must match the RTTI setting.
Decide whether the engine's own reflection or type-id system replaces RTTI and how it is enforced across plugins and third-party code.
## What RTTI is and what turning it off removes **Run-time type information** is the per-class data the compiler emits so that `typeid` and `dynamic_cast` can inspect the dynamic type of an object. GCC's manual describes the switch as disabling generation of that information for every class with virtual functions, to save space. With it off: - `typeid(expr)` is rejected; Apple clang 21 says `use of typeid requires -frtti`. - `dynamic_cast` to a derived class is rejected with `use of dynamic_cast requires -frtti`. Casts that need no run-time check (to `void*` or to an unambiguous base) still compile, as GCC's manual notes. - **Exceptions keep working.** The compiler still generates type information for every type that is thrown, because `catch (const Base&)` must match a thrown `Derived`. A test built with RTTI off caught a thrown derived class through a base reference as normal. The compilers also stop defining `__cpp_rtti`, which is what library headers test. ## Where the standard library used typeid Several standard members were specified in terms of `std::type_info`, so each library has to decide what to do with them: | Member | libc++ (LLVM 23) without RTTI | libstdc++ (GCC 16) without RTTI | |---|---|---| | `std::any::type()` | removed | removed | | `std::any_cast` | works | works | | `std::function::target_type()` | removed | removed | | `std::function::target<F>()` | removed | kept, compares manager-function addresses | | `std::get_deleter<D>(shared_ptr)` | removed | kept, always returns null | The libc++ entries were confirmed by compiling against it: `no member named 'target' in 'std::function<int (int)>'`, `no member named 'get_deleter' in namespace 'std'` and `no member named 'type' in 'std::any'`. The libstdc++ entries come from its headers, where the `get_deleter` body under `#if __cpp_rtti` asks the control block with `typeid(_Del)` and the `#else` branch is `return 0;`. ## Why the libstdc++ behaviour is the dangerous one A missing member is a compile error the engine team sees on day one. A member that compiles and returns a different answer is not. Consider an asset system that stores textures in `std::shared_ptr<Texture>` with a custom `GpuDeleter`, and a debug tool that calls `std::get_deleter<GpuDeleter>(ptr)` to find which pool a texture came from. Built with RTTI on, it finds the deleter. Built with RTTI off on libstdc++, every lookup returns `nullptr`, so the tool reports every texture as "not GPU-owned" and nothing fails to compile. Three habits keep this from biting: 1. Grep for `get_deleter`, `target_type`, `any::type` and `typeid` before turning RTTI off, and decide on each call site. 2. Build the engine against both libraries in CI if it ships on platforms that use different ones; one of them will at least fail loudly. 3. Prefer designs that never ask a type-erased object what it holds, such as keeping the deleter's pool pointer in the deleter itself and reaching it through the owning object. ## How std::any and std::function cope Type-erased wrappers need some identity for the stored type even without RTTI, and both libraries keep one that is just an address. libstdc++ compares the address of the per-type manager function that filled the `any` or `function`; libc++'s `std::any` compares the address of a per-type static object it keeps as a fallback id. `any_cast<int>` therefore asks "was this filled for `int`?" without ever touching `std::type_info`. That is why `std::any_cast` keeps working in both libraries and libstdc++ can keep `function::target`, while members whose contract is to return a `std::type_info` have nothing to return and disappear. ## What actually shrinks The saving is in data, not in code paths. In the Itanium C++ ABI that GCC and Clang use on most targets, each class with virtual functions gets a `std::type_info` object, its vtable points at that object, and the object points at the class's mangled name, stored as a string. A large engine has thousands of polymorphic classes, many of them template instantiations with long mangled names, so the strings add up. With RTTI off those objects and strings are emitted only for types that are actually thrown. The size win is real but modest, and depends on how many polymorphic types the code has; measure it on the engine's own binary. ## Mixing translation units GCC's manual also warns that mixing code compiled with and without RTTI may not work: a class compiled without RTTI used as a base for a class compiled with it can fail to link. Engines that ship a plugin SDK therefore have to publish the setting as part of the ABI, not as a private build detail.
- Why does catch (const Base&) still match a thrown Derived when RTTI is disabled?Exception matching needs type information for the thrown type, so the compiler keeps generating it for every type that is thrown, even with RTTI off. GCC's manual states this directly: exception handling uses the same information, generated as needed. Only `typeid` and checked `dynamic_cast` in user code are rejected.
- Which dynamic_cast expressions still compile in GCC with RTTI disabled?Casts that need no run-time type check: a cast to `void*` and a cast to an unambiguous base class. GCC's manual lists both. A downcast or cross-cast to a derived class needs the dynamic type and is rejected.
- Turning off RTTI also stops exceptions from matching by type.
- std::any_cast cannot work without typeid.
- Both standard libraries remove std::get_deleter when RTTI is off.
- Disabling RTTI only affects code that calls typeid directly.
- Objects built with and without RTTI always link together safely.