std::type_info::hash_code
提供: cppreference.com
std::size_t hash_code()constnoexcept; | (C++11以上) | |
同じ型を参照するすべての type_info
オブジェクトに対して、その hash_code() が同じになるような、未規定の値を返します。
他の保証は何もありません。 異なる型を参照する type_info
オブジェクトが同じ hash_code を持つ可能性もあります (処理系は可能な限りこれを回避することが推奨されてはいますが)。 また同じプログラムでも実行のたびに同じ型に対する hash_code が変わる可能性もあります。
目次 |
[編集]引数
(なし)
[編集]戻り値
同じ型を参照するすべての type_info
オブジェクトに対して同じであるような値。
[編集]例
以下のプログラムは std::type_index を使用せずに型と値を効率良くマップする例です。
Run this code
#include <iostream>#include <typeinfo>#include <unordered_map>#include <string>#include <functional>#include <memory> struct A {virtual ~A(){}}; struct B : A {};struct C : A {}; using TypeInfoRef =std::reference_wrapper<conststd::type_info>; struct Hasher {std::size_t operator()(TypeInfoRef code)const{return code.get().hash_code();}}; struct EqualTo {bool operator()(TypeInfoRef lhs, TypeInfoRef rhs)const{return lhs.get()== rhs.get();}}; int main(){std::unordered_map<TypeInfoRef, std::string, Hasher, EqualTo> type_names; type_names[typeid(int)]="int"; type_names[typeid(double)]="double"; type_names[typeid(A)]="A"; type_names[typeid(B)]="B"; type_names[typeid(C)]="C"; int i;double d; A a; // note that we're storing pointer to type Astd::unique_ptr<A> b(new B);std::unique_ptr<A> c(new C); std::cout<<"i is "<< type_names[typeid(i)]<<'\n';std::cout<<"d is "<< type_names[typeid(d)]<<'\n';std::cout<<"a is "<< type_names[typeid(a)]<<'\n';std::cout<<"b is "<< type_names[typeid(*b)]<<'\n';std::cout<<"c is "<< type_names[typeid(*c)]<<'\n';}
出力:
i is int d is double a is A b is B c is C
[編集]関連項目
(C++20で削除) | オブジェクトが同じ型を参照しているかどうか確認します (パブリックメンバ関数) |
型の処理系定義な名前 (パブリックメンバ関数) |