Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the lifetime of the object returned by typeid operator?

If I call typeid and retrieve the address of returned type_info:

const type_info* info = &( typeid( Something ) );

what's the lifetime of the object returned by typeid and how long will the pointer to that object remain valid?

like image 903
sharptooth Avatar asked Aug 11 '11 11:08

sharptooth


People also ask

What does Typeid return?

The typeid operator returns an lvalue of type const type_info that represents the type of our value.

What does Typeid return in C++?

The typeid operator returns an lvalue of type const std::type_info that represents the type of expression expr. You must include the standard template library header <typeinfo> to use the typeid operator. Classes A and B are polymorphic; classes C and D are not.

What is the use of Typeid () function?

The typeid operator allows the type of an object to be determined at run time. The result of typeid is a const type_info& . The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used.

Does Typeid require RTTI?

The typeid operator requires RunTime Type Identification (RTTI) to be generated, which must be explicitly specified at compile time through a compiler option.


2 Answers

However the implementation implements them, the results of typeid expressions are lvalues and the lifetime of the objects that those lvalues refer to must last until the end of the program.

From ISO/IEC 14882:2003 5.2.8 [expr.typeid]:

The result of a typeid expression is an lvalue [...] The lifetime of the object referred to by the lvalue extends to the end of the program.

like image 153
CB Bailey Avatar answered Sep 24 '22 08:09

CB Bailey


From 5.2.8.1 of C++ 2003 standard:

The result of a typeid expression is an lvalue of static type const std::type_info (18.5.1) and dynamic type const std::type_info or const name where name is an implementation-defined class derived from std::type_info which preserves the behavior described in 18.5.1.61) The lifetime of the object referred to by the lvalue extends to the end of the program. Whether or not the destructor is called for the type_info object at the end of the program is unspecified.

like image 22
Armen Tsirunyan Avatar answered Sep 25 '22 08:09

Armen Tsirunyan