Use data type (class type) as key in a map

C++11 solves this by providing std::type_index, in <typeindex>, which is a copyable, comparable and hashable object constructed from a std::type_info object that can be used as the key in associative containers.

(The implementation is fairly simple, so even if you don’t have C++11 yourself, you could steal the implementation from, say GCC 4.7, and use that in your own code.)

#include <typeindex>
#include <typeinfo>
#include <unordered_map>

typedef std::unordered_map<std::type_index, int> tmap;

int main()
{
    tmap m;
    m[typeid(main)] = 12;
    m[typeid(tmap)] = 15;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)