Table of Contents

<typeinfo>

<typeinfo> provides std::type_info, an object that holds type metadata available at runtime (via typeid()). You can compare type_info objects, get a name (though it's mangled), or use before() to sort types.

Use it for runtime type identification; wrap it in std::type_index if you need a hashable version.

Example

This example uses typeid to get runtime type information and check if values match specific types.

// compile: g++ -std=c++11 -o typeinfoexample typeinfoexample.cpp
// run: ./typeinfoexample
// description: runtime type identification
 
#include <typeinfo>
#include <iostream>
 
void identify(int) { std::cout << "got int\n"; }
void identify(double) { std::cout << "got double\n"; }
 
int main() {
    const std::type_info& ti = typeid(42);
    std::cout << "typeid(42): " << ti.name() << "\n";
 
    if (typeid(int) == typeid(42)) {
        std::cout << "42 is int\n";
    }
 
    return 0;
}