DekGenius.com
Team LiB   Previous Section   Next Section
typeid operator Runtime type identification operator

postfix-expr := typeid ( expression ) | typeid ( type-id )

The typeid operator returns a const reference to a type_info object that describes type-id or the type of expression. If expression is an lvalue (not a pointer) of a polymorphic class, the type_info of the most-derived class is returned. Otherwise, expression is not evaluated, and the type_info of its static type is returned.

Each distinct type has its own associated type_info object, but type synonyms (such as those created with typedef) have the same type_info object.

Example

template<typename T>
void debug(const T& obj)
{
  std::clog << typeid(obj).name(  ) << ':' << &obj << '\n';
}

See Also

expression, typedef, Chapter 3, <typeinfo>

    Team LiB   Previous Section   Next Section