Do C++ POD types have RTTI?
Asked Answered
B

1

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.

Bartholemy answered 11/2, 2016 at 14:38 Comment(4)
The pointer would be statically determined, right? Since there's nothing dynamic to consider.Radbun
"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.Katonah
AFAIK, no. see this codeWheels
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.Lucilius
L
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.

Lasso answered 11/2, 2016 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.