std::hash
<bitset>
<coroutine>
<chrono>
<filesystem>
<functional>
<memory>
<optional>
<stacktrace>
<string>
<string_view>
<system_error>
<text_encoding>
<thread>
<typeindex>
<utility>
<variant>
<vector>
struct hash;
The enabled specializations of the hash
template define a function object that implements a hash function.
Given a type Key
, each specialization std::hash<Key>
is either enabled or disabled :
- If
std::hash<Key>
is not provided by the program or the user, it is disabled. - Otherwise,
std::hash<Key>
is enabled if all following conditions are satisfied:
- All following requirements are satisfied:
- Hash (with
Key
as the function call argument type) - DefaultConstructible
- CopyAssignable
- Swappable
- Hash (with
- Given the following values:
- h, an object of type
std::hash<Key>
. - k1 and k2, objects of type
Key
.
- h, an object of type
- All following requirements are satisfied:
- If k1 == k2 is true, h(k1) == h(k2) is also true.
- Unless
std::hash<Key>
is a program-defined specialization, h(k1) will never throw an exception.
- Otherwise,
std::hash<Key>
is disabled.
Disabled specializations do not satisfy Hash, do not satisfy FunctionObject, and following values are all false:
- std::is_default_constructible <std::hash<Key>>::value
- std::is_copy_constructible <std::hash<Key>>::value
- std::is_move_constructible <std::hash<Key>>::value
- std::is_copy_assignable <std::hash<Key>>::value
- std::is_move_assignable <std::hash<Key>>::value
In other words, they exist, but cannot be used.
Contents
Nested types
argument_type
(deprecated in C++17)
Key
[edit] Member functions
[edit] Standard library specializations
Each header that declares the template std::hash
also provides enabled specializations of std::hash
for the following types:
- all cv-unqualified arithmetic types
- all cv-unqualified enumeration types
- all cv-unqualified pointer types
- std::nullptr_t
A freestanding implementation is required to provide these aforementioned specializations and the disabled-by-default specializations.
(since C++20)On top of that, some headers also provide other enabled std::hash
specializations for library types (see below).
For all std::hash
specializations provided by the standard library except the following, all their member functions are noexcept:
- std::hash<std::indirect>
- std::hash<std::chrono::duration>
- std::hash<std::chrono::time_point>
- std::hash<std::chrono::zoned_time>
[edit] Specializations for library types
Language support library
Dianostics library
Memory management library
General utilities library
Containers library
Strings library
(class template specialization) [edit]
Text processing library
Time library
Input/output library
Concurrency support library
[edit] 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.
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.
Additional specializations for std::pair and the standard container types, as well as utility functions to compose hashes are available in boost::hash
.
[edit] Example
#include <cstddef> #include <functional> #include <iomanip> #include <iostream> #include <string> #include <unordered_set> struct S { std::string first_name; std::string last_name; bool operator==(const S&) const = default; // since C++20 }; // Before C++20. // bool operator==(const S& lhs, const S& rhs) // { // return lhs.first_name == rhs.first_name && lhs.last_name == rhs.last_name; // } // Custom hash can be a standalone function object. struct MyHash { std::size_t operator()(const S& s) const noexcept { std::size_t h1 = std::hash<std::string >{}(s.first_name); std::size_t h2 = std::hash<std::string >{}(s.last_name); return h1 ^ (h2 << 1); // or use boost::hash_combine } }; // Custom specialization of std::hash can be injected in namespace std. template<> struct std::hash<S> { std::size_t operator()(const S& s) const noexcept { std::size_t h1 = std::hash<std::string >{}(s.first_name); std::size_t h2 = std::hash<std::string >{}(s.last_name); return h1 ^ (h2 << 1); // or use boost::hash_combine } }; int main() { std::string str = "Meet the new boss..."; std::size_t str_hash = std::hash<std::string >{}(str); std::cout << "hash(" << std::quoted (str) << ") =\t" << str_hash << '\n'; S obj = {"Hubert", "Farnsworth"}; // Using the standalone function object. std::cout << "hash(" << std::quoted (obj.first_name) << ", " << std::quoted (obj.last_name) << ") =\t" << MyHash{}(obj) << " (using MyHash) or\n\t\t\t\t" << std::hash<S>{}(obj) << " (using injected specialization)\n"; // Custom hash makes it possible to use custom types in unordered containers. // The example will use the injected std::hash<S> specialization above, // to use MyHash instead, pass it as a second template argument. std::unordered_set <S> names = {obj, {"Bender", "Rodriguez"}, {"Turanga", "Leela"}}; for (auto const& s: names) std::cout << std::quoted (s.first_name) << ' ' << std::quoted (s.last_name) << '\n'; }
Possible output:
hash("Meet the new boss...") = 10656026664466977650 hash("Hubert", "Farnsworth") = 12922914235676820612 (using MyHash) or 12922914235676820612 (using injected specialization) "Bender" "Rodriguez" "Turanga" "Leela" "Hubert" "Farnsworth"
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2119 | C++11 | specializations for extended integer types were missing | provided |
LWG 2148 | C++11 | specializations for enumerations were missing | provided |
LWG 2543 | C++11 | std::hash might not be SFINAE-friendly
|
made SFINAE-friendly |
LWG 2817 | C++11 | specialization for std::nullptr_t was missing | provided |