Skip to main content
Standard C++ does not possess a type(...) operator. Type querying and deduction are instead handled by two distinct standard operators: decltype(...) for compile-time type deduction, and typeid(...) for Run-Time Type Information (RTTI) and static type identification.

decltype(...)

The decltype operator inspects the declared type of an entity or the type and value category of an expression. It is evaluated entirely at compile-time and does not execute the expression it evaluates. Syntax:
Evaluation Rules:
  1. Unparenthesized id-expression or class member access: If the operand is an unparenthesized identifier or a member access expression (obj.member or ptr->member), decltype yields the exact declared type of the entity.
  2. Parenthesized expression or other expressions: If the operand is an expression of type T, the resulting type depends on the expression’s value category:
    • If the expression is an lvalue, decltype yields T&.
    • If the expression is an xvalue, decltype yields T&&.
    • If the expression is a prvalue, decltype yields T.
Mechanics Example:

typeid(...)

The typeid operator queries information about a type. The operator yields an lvalue of type const std::type_info (defined in the <typeinfo> header) that represents the type. Syntax:
Evaluation Rules:
  1. Type Operand: If the operand is a type-id, typeid evaluates at compile-time and yields the const std::type_info for that exact type.
  2. Non-Polymorphic Expression Operand: If the operand is an expression whose static type is not a polymorphic class (a class with at least one virtual function), typeid evaluates at compile-time. The expression is not executed.
  3. Polymorphic Expression Operand: If the operand is a glvalue expression of a polymorphic class type, typeid evaluates at run-time. It inspects the vtable to yield the const std::type_info of the most derived (dynamic) type of the object.
  4. Null Pointer Dereference: If the operand is a dereferenced null pointer of a polymorphic class type (e.g., typeid(*ptr) where ptr evaluates to a null pointer value), the operator throws a std::bad_typeid exception.
  5. CV-Qualifiers and References: typeid strips top-level const and volatile qualifiers and ignores references before evaluation. typeid(T&) and typeid(const T) both yield the const std::type_info for T.
Mechanics Example:
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More