Is type_info part of RTTI?

I asked a question. Do C ++ POD types have RTTI? , and someone told me in the comments:

POD types have type_info, but they do not have RTTI, and this is possible because type_info is not always RTTI.

and that seems right, since I can get type_info type POD (non-polymorphic).

But for now, I am compiling this simple program:

 #include <iostream> struct X { int a; }; int main() { using namespace std; std::cout << typeid(X) << std::endl; return 0; } 

with the -fno-rtti GCC flag:

 $ g++ -fno-rtti main.cpp && ./main 

It will not compile:

 main.cpp: In function 'int main()': main.cpp:12:26: error: cannot use typeid with -fno-rtti std::cout << typeid(X) << std::endl; ^ 

Does this mean that type_info is part of RTTI, or is it just GCC behavior?

+7
c ++ gcc rtti
source share
2 answers

annotation

RTTI itself is not really formal: C ++ only says what typeid and dynamic_cast , not how they are implemented. However, it is convenient to group such operations under a common name, which is RTTI.

Note that an implementation is not required to strictly obtain this information at runtime, i.e.

if ( typeid(int) == typeid(double) )

can also be determined during program evaluation, like std::is_same . int undeniably non-polymorphic (it does not have a "dynamic" type). cppreference even claims:

When applied to an expression of a polymorphic type, evaluation of the typeid expression may include runtime overhead (virtual table lookup), otherwise the typeid expression is allowed at compile time.

But this must be taken carefully.


Does this mean that type_info is part of RTTI, or is it just GCC behavior?

type_info is a class. You cannot create any object of this type - you can only through typeid .

-fno-rtti disable RTTI in GCC: you cannot use typeid and thus cannot type_info . They are very close to each other.

In conclusion, the original quote is perfectly correct:

POD types have type_info but no RTTI, and this is possible because type_info is not always RTTI.

Runtime information is available through typeid . There is nothing dynamic to consider (indeed, dynamic_cast does not make sense).

+2
source share

There is no concept of "RTTI" in the standard. Instead, he said in different words.

  • <typeinfo> is referred to as "dynamic type identification" in [support.general]

  • [intro.object] says: "Some objects are polymorphic (10.3), the implementation generates information associated with each such object, which allows you to determine the type of objects during program execution"

  • [expr.dynamic.cast] talks about checks that occur in run-time mode. All other uses of "runtime" in the standard refer to something else.

[expr.typeid] explains what typeid does:

2 When typeid is applied to a glvalue expression whose type is the polymorphic type of the class (10.3), the result refers to a std::type_info object representing the type of the derived object itself (1.8) (which is a dynamic type), to which glvalue belongs ... .

The latter usually refers to a run-time operation.

3 When typeid is applied to an expression other than gl the polymorphic type of the class, the result refers to the std::type_info representing the static type of the expression .... The expression is an unvalued operand (section 5).

While the former can be seen as a compile-time operation.

No matter what GCC really cares about and disables typeid and dynamic_cast in general if you use the -fno-rtti flag:

rtti.c :

  if (! flag_rtti) { error ("cannot use typeid with -fno-rtti"); return false; } 

Can it disable typeid only for polymorphic types? Of course. But we are going to go with an occam razor in that it is much easier to develop to prevent using typeid at all.

+1
source share

All Articles