Search code examples
c++rttivtable

Do C++ POD types have RTTI?


As I understand how RTTI is implemented in various C++ compilers (such as GCC), a pointer to the type_info data is stored in the vtable data of each class.

And also as mentioned here, POD type may not have a vtable.

But if POD types may not have a vtable then where is the pointer to the type_info stored? I know it is implementation-specific, but it would be better to be aware of a C++ compiler (such as GCC) internals.


Solution

  • There are two kinds of types (for the purposes of RTTI): polymorphic types and non-polymorphic types. A polymorphic type is a type that has a virtual function, in itself or inherited from a base class. A non-polymorphic type is everything else; this includes POD types, but it includes many other types too.

    If you have a pointer/reference to a polymorphic type T, and you call typeid on it, the type_info you get back is not necessarily the type_info you would get back for typeid(T{}). Instead, it is the true dynamic type of the object: the most derived class.

    If you have a pointer/reference to a non-polymorphic type T, typeid will always return the type_info for T itself. Non-polymorphic types always assume that the pointer/reference is exactly its static type.

    POD types are non-polymorphic, but a huge number of other types are non-polymorphic as well.