5

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.

Community
  • 1
  • 1
Sadeq
  • 7,795
  • 4
  • 34
  • 45
  • 9
    The pointer would be statically determined, right? Since there's nothing dynamic to consider. – Kerrek SB Feb 11 '16 at 14:38
  • 1
    "it whould be better to be aware of a C++ compiler (such as GCC) internals." Well, both GCC and Clang are open source, so you can just look into it. – Revolver_Ocelot Feb 11 '16 at 14:49
  • 1
    AFAIK, no. [see this code](http://ideone.com/2ZGeJS) – leemes Feb 11 '16 at 15:08
  • 4
    To clarify Kerrek's statement: POD types do have `type_info`, but don't have RTTI, and that's possible because `type_info` isn't always RTTI. – MSalters Feb 11 '16 at 15:19

1 Answers1

8

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.

Qqwy
  • 5,214
  • 5
  • 42
  • 83
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982