Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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:
decltype(entity)
decltype(expression)
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:
#include <utility>

int x = 0;
const int cx = 0;
int& rx = x;
struct S { int m; };
S s;

using T1 = decltype(x);            // Yields: int
using T2 = decltype(cx);           // Yields: const int
using T3 = decltype(rx);           // Yields: int&
using T4 = decltype(s.m);          // Yields: int (unparenthesized member access)
using T5 = decltype((s.m));        // Yields: int& (parenthesized lvalue expression)
using T6 = decltype(x + 1);        // Yields: int (prvalue expression)
using T7 = decltype(std::move(x)); // Yields: int&& (xvalue expression)

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:
typeid(type)
typeid(expression)
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:
#include <typeinfo>

struct Base { virtual ~Base() = default; };
struct Derived : Base {};
struct NonPolyBase {};
struct NonPolyDerived : NonPolyBase {};

int x = 5;
const int& rx = x;

// Compile-time evaluation
const std::type_info& t1 = typeid(int);
const std::type_info& t2 = typeid(rx);        // Yields type_info for 'int' (cv-ref stripped)
const std::type_info& t3 = typeid(x + 1.5);   // Yields type_info for 'double'

// Compile-time vs Run-time evaluation
Derived d;
Base& b_ref = d;
NonPolyDerived npd;
NonPolyBase& npb_ref = npd;

const std::type_info& t4 = typeid(b_ref);     // Run-time: Yields type_info for 'Derived' (polymorphic)
const std::type_info& t5 = typeid(npb_ref);   // Compile-time: Yields type_info for 'NonPolyBase' (static type)

// Null pointer dereference
Base* null_ptr = nullptr;
// const std::type_info& t6 = typeid(*null_ptr); // Throws std::bad_typeid at run-time
Master C++ with Deep Grasping Methodology!Learn More