Skip to content

Commit 6f4d624

Browse files
committed
feat(std::hash): page for operator()
1 parent 0915d58 commit 6f4d624

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

src/content/docs/cpp/library/utility/hash.mdx

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Defined in header <CppHeader name="vector" />.
4848
<Decl slot="decl">
4949
<RevisionBlock since="C++11" noborder>
5050
```cpp
51-
template< class Key >
51+
template<class Key>
5252
struct hash;
5353
```
5454
</RevisionBlock>
@@ -68,10 +68,10 @@ Given a type `Key`, each specialization `std::hash<Key>` is either _enabled_ or
6868
- [Swappable](../named_req/Swappable.html "cpp/named req/Swappable")
6969

7070
- Given the following values:
71-
- h, an object of type `std::hash<Key>`.
72-
- k1 and k2, objects of type `Key`.
71+
- `h`, an object of type `std::hash<Key>`.
72+
- `k1` and `k2`, objects of type `Key`.
7373
All following requirements are satisfied:
74-
- If k1 \== k2 is true, h(k1) \== h(k2) is also true.
74+
- If `k1 == k2` is true, `h(k1) == h(k2)` is also true.
7575
- Unless `std::hash<Key>` is a [program-defined specialization](../language/type-id.html#Program-defined_type "cpp/language/type"), h(k1) will never throw an exception.
7676

7777
- Otherwise, `std::hash<Key>` is disabled.
@@ -100,8 +100,8 @@ In other words, they exist, but cannot be used.
100100
### Member functions
101101
| | |
102102
|---|---|
103-
|`constuctor` | constructs a hash function object <br/> (public member function)|
104-
|`operator()` | calculates the hash of the argument <br/> (public member function)|
103+
|(constuctor) | <Desc kind="public member function">constructs a hash function object </Desc>|
104+
|[`operator()`](./operator-call/) | <Desc kind="public member function">calculates the hash of the argument </Desc>|
105105

106106

107107
### Standard library specializations
@@ -225,18 +225,6 @@ hash("Hubert", "Farnsworth") = 12922914235676820612 (using MyHash) or
225225
"Hubert" "Farnsworth"
226226
```
227227

228-
```cpp
229-
// Simple move constructor
230-
// the expression "arg.member" is lvalue
231-
A(A&& arg) : member(std::move(arg.member)) {}
232-
233-
// Simple move assignment operator
234-
A& operator=(A&& other) {
235-
member = std::move(other.member);
236-
return *this;
237-
}
238-
```
239-
240228
## Defect reports
241229

242230
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: std::hash::operator()
3+
cppdoc:
4+
revision:
5+
since: C++11
6+
---
7+
8+
import { ParamDoc, ParamDocList } from "@components/param-doc";
9+
10+
Specializations of `std::hash` should define an `operator()` that:
11+
12+
- Takes a single argument key of type `Key`.
13+
- Returns a value of type `std::size_t` that represents the hash value of key.
14+
- For two parameters `k1` and `k2` that are equal, `std::hash<Key>()(k1) == std::hash<Key>()(k2)`.
15+
- 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<size_t>::max()`.
16+
17+
18+
## Parameters
19+
<ParamDocList>
20+
<ParamDoc name="key">
21+
the object to be hashed
22+
</ParamDoc>
23+
</ParamDocList>
24+
25+
## Return value
26+
A `std::size_t` representing the hash value.
27+
28+
## Exceptions
29+
Hash functions should not throw exceptions.
30+
31+
## Example
32+
The following code shows how to specialize the std::hash template for a custom class. The hash function uses Fowler–Noll–Vo hash algorithm.
33+
34+
```cpp
35+
#include <cstdint>
36+
#include <functional>
37+
#include <iostream>
38+
#include <string>
39+
40+
struct Employee
41+
{
42+
std::string name;
43+
std::uint64_t ID;
44+
};
45+
46+
namespace std
47+
{
48+
template <>
49+
class hash<Employee>
50+
{
51+
public:
52+
std::uint64_t operator()(const Employee& employee) const
53+
{
54+
// computes the hash of an employee using a variant
55+
// of the Fowler-Noll-Vo hash function
56+
constexpr std::uint64_t prime{0x100000001B3};
57+
std::uint64_t result{0xcbf29ce484222325};
58+
59+
for (std::uint64_t i{}, ie = employee.name.size(); i != ie; ++i)
60+
result = (result * prime) ^ employee.name[i];
61+
62+
return result ^ (employee.ID << 1);
63+
}
64+
};
65+
}
66+
67+
int main()
68+
{
69+
Employee employee;
70+
employee.name = "Zaphod Beeblebrox";
71+
employee.ID = 42;
72+
73+
std::hash<Employee> hash_fn;
74+
std::cout << hash_fn(employee) << '\n';
75+
}
76+
```
77+
78+
Output:
79+
80+
```cpp
81+
12615575401975788567
82+
```
83+

0 commit comments

Comments
 (0)