C++0x std::type_index

C++03のstd::type_infoでは、unordered連想コンテナのキーにすることはできません。
C++0xではこの問題を解決するため、std::type_indexというクラスが提供されます。


おそらく、以下のように使用することになるでしょう。

#include <iostream>
#include <typeinfo>
#include <unordered_map>

using namespace std;

int main()
{
    unordered_map<type_index, int> m;

    m[typeid(int)]    = 1;
    m[typeid(double)] = 2;

    cout << m[typeid(int)] << endl; // 1
}

また、std::type_infoにはhash_codeというメンバ関数が追加されるので
unordered連想コンテナでのキーの比較にはこのメンバ関数が使用されます。


N2530 Making It Easier to Use std::type_info as an Index in an Associative Container

C++0x言語拡張まとめ