Site Tools


wiki:hpp-typeindex

Table of Contents

<typeindex>

<typeindex> provides std::type_index, a hashable wrapper around std::type_info from <typeinfo>. Use it to store types in containers like std::map or std::unordered_map.

You rarely need this; it's mainly useful for dynamic type dispatch or plugin systems.

Example

This example uses type_index as a map key to associate types with their string names.

// compile: g++ -std=c++11 -o typeindexexample typeindexexample.cpp
// run: ./typeindexexample
// description: use types as map keys
 
#include <typeindex>
#include <map>
#include <iostream>
 
int main() {
    std::map<std::type_index, std::string> type_names;
 
    type_names[std::type_index(typeid(int))] = "integer";
    type_names[std::type_index(typeid(double))] = "floating point";
 
    std::cout << "int is: " << type_names[std::type_index(typeid(int))] << "\n";
 
    return 0;
}
wiki/hpp-typeindex.md · Last modified: by 127.0.0.1