`typeid` — C++ Keyword

`typeid` — C++ Keyword

The typeid keyword in C++: queries the runtime type of an expression.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

typeid

A compile-time and runtime operator that returns a std::type_info reference describing the type or dynamic type of an expression. Defined in <typeinfo>.

Syntax

typeid(expression)
typeid(type-name)

Example

#include <print>
#include <typeinfo>
#include <memory>

struct Animal { virtual ~Animal() = default; };
struct Dog : Animal {};
struct Cat : Animal {};

int main() {
    // Static type query
    int n = 0;
    std::println("{}", typeid(n).name());         // "i" (int) on GCC

    // Dynamic type query through base pointer
    std::unique_ptr<Animal> a = std::make_unique<Dog>();
    if (typeid(*a) == typeid(Dog)) {
        std::println("It's a Dog");   // executed
    }

    std::unique_ptr<Animal> b = std::make_unique<Cat>();
    std::println("{}", typeid(*b).name());   // Cat's mangled name
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}