std::type_info::hash_code (3) - Linux Manuals
std::type_info::hash_code: std::type_info::hash_code
Command to display std::type_info::hash_code
manual in Linux: $ man 3 std::type_info::hash_code
NAME
std::type_info::hash_code - std::type_info::hash_code
Synopsis
size_t hash_code() const; (since C++11)
Returns an unspecified value such that for all type_info objects referring to the same type, their hash_code() is the same.
No other guarantees are given: type_info objects referring to different types may have the same hash_code (although the standard recommends that implementations avoid this as much as possible), and hash_code for the same type can change between invocations of the same program.
Parameters
(none)
Return value
A value that is identical for all type_info objects referring to the same type.
Example
The following program is an example of an efficient type-value mapping without using 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<const std::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 A
std::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';
}
Output:
i is int
d is double
a is A
b is B
c is C
See also
checks whether the objects refer to the same type
operator== (public member function)
operator!=
implementation defined name of the type
name (public member function)
Pages related to std::type_info::hash_code
- std::type_info::before (3) - std::type_info::before
- std::type_info::name (3) - std::type_info::name
- std::type_info::operator==,std::type_info::operator!= (3) - std::type_info::operator==,std::type_info::operator!=
- std::type_info::~type_info (3) - std::type_info::~type_info
- std::type_info (3) - std::type_info
- std::type_index (3) - std::type_index
- std::type_index::hash_code (3) - std::type_index::hash_code
- std::type_index::name (3) - std::type_index::name
- std::type_index::operator==,!=,<,<=,>,>= (3) - std::type_index::operator==,!=,<,<=,>,>=
- std::type_index::type_index (3) - std::type_index::type_index
- std::type_identity (3) - std::type_identity
- std::tan(std::complex) (3) - std::tan(std::complex)