std::type_index - cppreference.com
提供: cppreference.com
<tbody> </tbody>
type_index クラスは、連想コンテナおよび非順序連想コンテナでインデックスとして使用できる、 std::type_info オブジェクトのラッパークラスです。 type_info オブジェクトとの関係はポインタによって維持されます。 そのため type_index は CopyConstructible かつ CopyAssignable です。
メンバ関数
| オブジェクトを構築します (パブリックメンバ関数) [edit] | |
デストラクタ (暗黙に宣言) |
type_index オブジェクトを破棄します (パブリックメンバ関数) |
operator= (暗黙に宣言) |
type_index オブジェクトを代入します (パブリックメンバ関数) |
| ベースとなる std::type_info オブジェクトを比較します (パブリックメンバ関数) [edit] | |
| ハッシュコードを返します (パブリックメンバ関数) [edit] | |
| ベースとなる type_info オブジェクトに紐づけられている型の処理系定義の名前を返します (パブリックメンバ関数) [edit] |
ヘルパークラス
例
以下のプログラムは効率の良い型と値のマップの例です。
#include <iostream> #include <typeinfo> #include <typeindex> #include <unordered_map> #include <string> #include <memory> struct A { virtual ~A() {} }; struct B : A {}; struct C : A {}; int main() { std::unordered_map<std::type_index, std::string> type_names; type_names[std::type_index(typeid(int))] = "int"; type_names[std::type_index(typeid(double))] = "double"; type_names[std::type_index(typeid(A))] = "A"; type_names[std::type_index(typeid(B))] = "B"; type_names[std::type_index(typeid(C))] = "C"; int i; double d; A a; // note that we're storing pointer to type A std::unique_ptr<A> b(new B); std::unique_ptr<A> c(new C); std::cout << "i is " << type_names[std::type_index(typeid(i))] << '\n'; std::cout << "d is " << type_names[std::type_index(typeid(d))] << '\n'; std::cout << "a is " << type_names[std::type_index(typeid(a))] << '\n'; std::cout << "b is " << type_names[std::type_index(typeid(*b))] << '\n'; std::cout << "c is " << type_names[std::type_index(typeid(*c))] << '\n'; }
出力:
i is int d is double a is A b is B c is C