std::hash (3) - Linux Manuals
std::hash: std::hash
NAME
Synopsis
Defined in header <functional>
template< class Key > (since C++11)
struct hash;
Each specialization of this template is either enabled ("untainted") or disabled ("poisoned"). For every type Key for which neither the library nor the user provides an enabled specialization std::hash<Key>, that specialization exists and is disabled. Disabled specializations do not satisfy Hash, do not satisfy FunctionObject, and std::is_default_constructible_v, std::is_copy_constructible_v, std::is_move_constructible_v, std::is_copy_assignable_v, std::is_move_assignable_v are all false. In other words, they exist, but cannot be used. (since C++17)
The
enabled specializations of the
(since C++17) hash template defines a function object that implements a hash_function. Instances of this function object satisfy Hash. In particular, they define an operator() const that:
1. Accepts a single parameter of type Key.
2. Returns a value of type std::size_t that represents the hash value of the parameter.
3. Does not throw exceptions when called.
4. For two parameters k1 and k2 that are equal, std::hash<Key>()(k1) == std::hash<Key>()(k2).
5. For two different parameters k1 and k2 that are not equal, the probability that std::hash<Key>()(k1) == std::hash<Key>()(k2) should be very small, approaching 1.0/std::numeric_limits<std::size_t>::max().
All explicit and partial specializations of hash provided by the standard library are DefaultConstructible, CopyAssignable, Swappable and Destructible. User-provided specializations of hash also must meet those requirements.
The unordered associative containers std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap use specializations of the template std::hash as the default hash function.
Notes
The actual hash functions are implementation-dependent and are not required to fulfill any other quality criteria except those specified above. Notably, some implementations use trivial (identity) hash functions which map an integer to itself. In other words, these hash functions are designed to work with unordered associative containers, but not as cryptographic hashes, for example.
Hash functions are only required to produce the same result for the same input within a single execution of a program; this allows salted hashes that prevent collision denial-of-service attacks. (since C++14)
There is no specialization for C strings. std::hash<const char*> produces a hash of the value of the pointer (the memory address), it does not examine the contents of any character array.
Member types
Member type Definition
argument_type(deprecated in C++17) Key
result_type(deprecated in C++17) std::size_t
Member functions
constructor (public member function)
operator() (public member function)
Standard specializations for basic types
Defined in header <functional>
template<> struct hash<bool>;
template<> struct hash<char>;
template<> struct hash<signed char>;
template<> struct hash<unsigned char>;
template<> struct hash<char16_t>;
template<> struct hash<char32_t>;
template<> struct hash<wchar_t>;
template<> struct hash<short>;
template<> struct hash<unsigned short>;
template<> struct hash<int>;
template<> struct hash<unsigned int>;
template<> struct hash<long>;
template<> struct hash<long long>;
template<> struct hash<unsigned long>;
template<> struct hash<unsigned long long>;
template<> struct hash<float>;
template<> struct hash<double>;
template<> struct hash<long double>;
template< class T > struct hash<T*>;
In addition to the above, the standard library provides specializations for all (scoped and unscoped) enumeration types. These may be (but are not required to be) implemented as std::hash<std::underlying_type<Enum>::type>) (since C++14)
Each standard library header that declares the template std::hash provides enabled specializations of std::hash for std::nullptr_t and all cv-unqualified arithmetic types (including any extended integer types), all enumeration types, and all pointer types. (since C++17)
All member functions of all standard library specializations of this template are noexcept except for the member functions of std::hash<std::optional>, std::hash<std::variant>, and std::hash<std::unique_ptr>
Standard specializations for library types
std::hash<std::string>
std::hash<std::u8string>
std::hash<std::u16string>
std::hash<std::u32string>
std::hash<std::wstring>
std::hash<std::pmr::string>
std::hash<std::pmr::u8string>
std::hash<std::pmr::u16string>
std::hash<std::pmr::u32string>
std::hash<std::pmr::wstring> hash support for strings
(C++11)
(C++20)
(C++11)
(C++11)
(C++11)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
std::hash<std::error_code> hash support for std::error_code
(C++11)
std::hash<std::bitset> hash support for std::bitset
(C++11)
std::hash<std::unique_ptr> hash support for std::unique_ptr
(C++11)
std::hash<std::shared_ptr> hash support for std::shared_ptr
(C++11)
std::hash<std::type_index> hash support for std::type_index