名前空間
変種
操作

std::type_info::hash_code

提供: cppreference.com
< cpp‎ | types‎ | type info
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ(C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
型サポート
型の性質
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20未満)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
定数評価文脈
サポートされている操作
関係と性質の問い合わせ
型変更
型変換
(C++11)
(C++11)
(C++17)
(C++11)(C++20未満)(C++17)
 
 
std::size_t hash_code()constnoexcept;
(C++11以上)

同じ型を参照するすべての type_info オブジェクトに対して、その hash_code() が同じになるような、未規定の値を返します。

他の保証は何もありません。 異なる型を参照する type_info オブジェクトが同じ hash_code を持つ可能性もあります (処理系は可能な限りこれを回避することが推奨されてはいますが)。 また同じプログラムでも実行のたびに同じ型に対する hash_code が変わる可能性もあります。

目次

[編集]引数

(なし)

[編集]戻り値

同じ型を参照するすべての type_info オブジェクトに対して同じであるような値。

[編集]

以下のプログラムは std::type_index を使用せずに型と値を効率良くマップする例です。

#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で削除)
オブジェクトが同じ型を参照しているかどうか確認します
(パブリックメンバ関数)[edit]
型の処理系定義な名前
(パブリックメンバ関数)[edit]
close