typeid
operatorGeneral topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
General | ||||
Literals | ||||
Operators | ||||
Conversions | ||||
Queries information of a type.
Used where thedynamic type of apolymorphic object must be known and for static type identification.
Contents |
typeid ( type) | (1) | ||||||||
typeid ( expression) | (2) | ||||||||
Thetypeid expression is anlvalue expression which refers to an object withstatic storage duration, of const-qualified version of the polymorphic typestd::type_info or some type derived from it.
If the standard library definition ofstd::type_info is not visible when usingtypeid, the program is ill-formed.
Iftype or the type ofexpression is a class type or a reference to a class type, then that class type cannot be anincomplete type.
| (since C++17) |
Iftype or the type ofexpression is cv-qualified, the result of thetypeid refers to astd::type_info object representing the cv-unqualified type (that is,typeid(const T)==typeid(T)).
Iftypeid is used on an object under construction or destruction (in a destructor or in a constructor, including constructor'sinitializer list ordefault member initializers), then thestd::type_info object referred to by thistypeid represents the class that is being constructed or destroyed even if it is not the most-derived class.
When applied to an expression of polymorphic type, evaluation of a typeid expression may involve runtime overhead (a virtual table lookup), otherwise typeid expression is resolved at compile time.
It is unspecified whether the destructor for the object referred to bytypeid is executed at the end of the program.
There is no guarantee that the samestd::type_info object will be referred to by all evaluations of the typeid expression on the same type, although they would compare equal,std::type_info::hash_code of thosetype_info
objects would be identical, as would be theirstd::type_index.
conststd::type_info& ti1=typeid(A);conststd::type_info& ti2=typeid(A); assert(&ti1==&ti2);// not guaranteedassert(ti1== ti2);// guaranteedassert(ti1.hash_code()== ti2.hash_code());// guaranteedassert(std::type_index(ti1)==std::type_index(ti2));// guaranteed
The example showing output using one of the implementations wheretype_info::name returns full type names; filter through c++filt -t if using gcc or similar.
#include <iostream>#include <string>#include <typeinfo> struct Base{};// non-polymorphicstruct Derived: Base{}; struct Base2{virtualvoid foo(){}};// polymorphicstruct Derived2: Base2{}; int main(){int myint=50;std::string mystr="string";double*mydoubleptr= nullptr; std::cout<<"myint has type: "<<typeid(myint).name()<<'\n'<<"mystr has type: "<<typeid(mystr).name()<<'\n'<<"mydoubleptr has type: "<<typeid(mydoubleptr).name()<<'\n'; // std::cout << myint is a glvalue expression of polymorphic type; it is evaluatedconststd::type_info& r1=typeid(std::cout<< myint);// side-effect: prints 50std::cout<<'\n'<<"std::cout<<myint has type : "<< r1.name()<<'\n'; // std::printf() is not a glvalue expression of polymorphic type; NOT evaluatedconststd::type_info& r2=typeid(std::printf("%d\n", myint));std::cout<<"printf(\"%d\\n\",myint) has type : "<< r2.name()<<'\n'; // Non-polymorphic lvalue is a static type Derived d1; Base& b1= d1;std::cout<<"reference to non-polymorphic base: "<<typeid(b1).name()<<'\n'; Derived2 d2; Base2& b2= d2;std::cout<<"reference to polymorphic base: "<<typeid(b2).name()<<'\n'; try{// dereferencing a null pointer: okay for a non-polymorphic expressionstd::cout<<"mydoubleptr points to "<<typeid(*mydoubleptr).name()<<'\n';// dereferencing a null pointer: not okay for a polymorphic lvalue Derived2* bad_ptr= nullptr;std::cout<<"bad_ptr points to... ";std::cout<<typeid(*bad_ptr).name()<<'\n';}catch(conststd::bad_typeid& e){std::cout<<" caught "<< e.what()<<'\n';}}
Possible output:
======== output from Clang ========myint has type: imystr has type: NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEmydoubleptr has type: Pd50std::cout<<myint has type : NSt3__113basic_ostreamIcNS_11char_traitsIcEEEEprintf("%d\n",myint) has type : ireference to non-polymorphic base: 4Basereference to polymorphic base: 8Derived2mydoubleptr points to dbad_ptr points to... caught std::bad_typeid ======== output from MSVC ========myint has type: intmystr has type: class std::basic_string<char,struct std::char_traits<char>,⮠class std::allocator<char> >mydoubleptr has type: double * __ptr6450std::cout<<myint has type : class std::basic_ostream<char,struct std::char_traits<char> >printf("%d\n",myint) has type : intreference to non-polymorphic base: struct Basereference to polymorphic base: struct Derived2mydoubleptr points to doublebad_ptr points to... caught Attempted a typeid of nullptr pointer!
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 492 | C++98 | whentypeid is applied to a reference to cv-qualified type, the result represented the referenced type | the result represents the cv-unqualified referenced type |
CWG 1416 | C++98 | the wording regarding top-level cv-qualification might be misinterpreted | improved the wording |
CWG 1431 | C++98 | typeid was only allowed to throwstd::bad_typeid | allowed to throw matchable derived classes |
CWG 1954 | C++98 | it was unclear whether null pointer dereference can be checked in subexpressions ofexpression | only checked at top level |
contains some type’s information, the class returned by the typeid operator (class)[edit] |