In computer programming, run-time type information or run-time type identification (RTTI) refers to a C++ mechanism that exposes information about an object's data type at runtime. Run-time type information can apply to simple data types, such as integers and characters, or to generic types. This is a C++ specialization of a more general concept called type introspection. Similar mechanisms are also known in other programming languages, such as Object Pascal (Delphi).
In the original C++ design, Bjarne Stroustrup did not include run-time type information, because he thought this mechanism was often misused.
The dynamic_cast<>
operation and typeid
operator in C++ are part of RTTI.
The C++ run-time type information permits performing safe typecasts and manipulating type information at run time.
RTTI is available only for classes which are polymorphic, which means they have at least one virtual method. In practice, this is not a limitation because base classes must have a virtual destructor to allow objects of derived classes to perform proper cleanup if they are deleted from a base pointer.
RTTI is optional with some compilers; the programmer can choose at compile time whether to include the function. There may be a resource cost to making RTTI available even if the program does not use it.
The typeid
keyword is used to determine the class of an object at run time. It returns a reference to std::type_info
object, which exists until the end of the program. The use of typeid
, in a non-polymorphic context, is often preferred over dynamic_cast<class_type>
in situations where just the class information is needed, because typeid
is a constant-time procedure, whereas dynamic_cast
must traverse the class derivation lattice of its argument at runtime. Some aspects of the returned object are implementation-defined, such as std::type_info::name()
, and cannot be relied on across compilers to be consistent.