public member function

std::type_info::hash_code

<typeinfo>
size_t hash_code() const noexcept;
Get type hash code
Returns a value that uniquely identifies the type.

This function returns the same value for any two type_info objects that compare equal, and different values for distinct types that do not.

The particular values returned are implementation-defined and may vary between executions of the same program.

Parameters

none

Return Value

A value that uniquely identifies the type (runtime constant).
size_t is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// type_info::hash_code example
#include <iostream>       // std::cout
#include <typeinfo>       // operator typeid
#include <unordered_map>  // std::unordered_map
#include <string>         // std::string

int main() {
  // map hash_codes to names (strings):
  std::unordered_map <std::size_t,std::string> type_names;
  type_names[typeid(int).hash_code()] = "integer";
  type_names[typeid(char).hash_code()] = "chararacter";

  int i;
  char c;
  std::cout << "i is " << type_names[typeid(i).hash_code()] << '\n';
  std::cout << "c is " << type_names[typeid(c).hash_code()] << '\n';

  return 0;
}


Output:
i is integer
c is character

Exception safety

No-throw guarantee: this member function never throws exceptions.

See also