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?
The typeid operator returns an lvalue of type const type_info that represents the type of our value.
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.
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.
The typeid operator requires RunTime Type Identification (RTTI) to be generated, which must be explicitly specified at compile time through a compiler option.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With