The typeid keyword

suggest change

The typeid keyword is a unary operator that yields run-time type information about its operand if the operand’s type is a polymorphic class type. It returns an lvalue of type const std::type_info. Top-level cv-qualification are ignored.

struct Base {
    virtual ~Base() = default;
};
struct Derived : Base {};
Base* b = new Derived;
assert(typeid(*b) == typeid(Derived{})); // OK

typeid can also be applied to a type directly. In this case, first top-level references are stripped, then top-level cv-qualification is ignored. Thus, the above example could have been written with typeid(Derived) instead of typeid(Derived{}):

assert(typeid(*b) == typeid(Derived{})); // OK

If typeid is applied to any expression that is not of polymorphic class type, the operand is not evaluated, and the type info returned is for the static type.

struct Base {
    // note: no virtual destructor
};
struct Derived : Base {};
Derived d;
Base& b = d;
assert(typeid(b) == typeid(Base)); // not Derived
assert(typeid(std::declval<Base>()) == typeid(Base)); // OK because unevaluated

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


RTTI:
* The typeid keyword

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype