The C++ type identification library is the part of the standard library that lets a program identify types at run time. It consists of the typeid operator, the std::type_info class in <typeinfo>, and the wrapper std::type_index in <typeindex>. It works together with run-time type information (RTTI) and dynamic_cast. Compile-time type inspection is a different tool and lives in <type_traits>.
The key rule is that typeid reports the dynamic type only for objects of polymorphic classes, meaning classes with at least one virtual function. For everything else it reports the static type.
typeid and std::type_info workdynamic_caststd::type_index as a map keytypeid and std::type_info worktypeid(expression) or typeid(type) returns a reference to a const std::type_info object. The object has a lifetime that lasts until the end of the program, so you can keep the reference.
The members of std::type_info are small:
| Member | Meaning |
|---|---|
operator==, operator!= | Compare two types for identity |
before(other) | Implementation-defined ordering, useful for sorting |
hash_code() | A hash that is equal for equal types |
name() | An implementation-defined name string |
The string from name() is not portable. GCC and Clang return a mangled name (i for int), while MSVC returns a readable one (int). Do not compare names across compilers or show them to users without demangling.
For a polymorphic class, typeid(*ptr) evaluates the object and returns its most derived type. For a class without virtual functions, it returns the static type of the expression. A common mistake is forgetting the virtual function.
The last delete is deliberately shown as a bug. Deleting a derived object through a base pointer without a virtual destructor is undefined behaviour, so always give polymorphic bases a virtual destructor. In real code, prefer std::unique_ptr<Base> over raw new and delete.
If you apply typeid to a null pointer dereference of a polymorphic type, such as typeid(*nullPtr), it throws std::bad_typeid.
dynamic_castMost code that asks "what type is this?" really wants to use the object as that type. dynamic_cast does the check and the conversion in one step and is usually preferable to comparing typeid results.
With pointers, a failed dynamic_cast yields nullptr. With references it throws std::bad_cast. Note that typeid(x) == typeid(T) is an exact match, while dynamic_cast<T*> also succeeds for classes derived from T.
std::type_index as a map keystd::type_info cannot be copied, so it does not work directly as a key in a container. std::type_index wraps a reference to it and provides comparison and hashing, which makes it ideal for registries and dispatch tables.
RTTI adds type metadata for polymorphic classes and can be switched off, for example with -fno-rtti in GCC and Clang. Some embedded and game projects do this to reduce binary size. With RTTI disabled, typeid on polymorphic types and dynamic_cast are not available, so you must use another mechanism such as a virtual kind() function, an enum tag, or std::variant with std::visit.
For closed sets of types, std::variant is often a better design than a hierarchy plus dynamic_cast, because the compiler checks that every case is handled.
typeid?Type traits in <type_traits> answer questions at compile time and cost nothing at run time. typeid answers questions about the dynamic type at run time and needs RTTI.
typeid(...).name() print strange text?On GCC and Clang the name is mangled. You can demangle it with abi::__cxa_demangle from <cxxabi.h>, which is a compiler-specific facility, not standard C++.
typeid work on a class without virtual functions?Yes, but it returns the static type of the expression, not the type of the object behind a base pointer.
typeid or dynamic_cast?Use dynamic_cast when you need to use the object as a derived type. Use typeid when you need an exact type match or a type key, for example in a registry.
The type identification library is typeid, std::type_info and std::type_index. It reports dynamic types only for polymorphic classes, relies on RTTI, and returns non-portable names. Prefer dynamic_cast for downcasts, std::type_index for type-keyed maps, and std::variant when you control the set of types.