BDE 4.14.0 Production release
|
Provide hashAppend
for std::variant
.
This component provides a free function template, bslh::hashAppend
, overloaded for the std::variant
class template. Including this function allows for std::variant
types (and types that contain them) to be used as keys in BDE hashed containers.
Note that use of this component requires that the language standard be 2017 or later, as that is when std::variant
first appears.
This section illustrates intended usage of this component.
Suppose we want to maintain a value as either an integer or a floating-point number, and have it fit within the BDE hash framework. We can use std::variant<int, double>
for this, and demonstrate that such a value can be correctly hashed.
First, we set up a pair of such optional values to represent the two potential states we wish to represent.
Then, we create a hashing object.
Next, we hash each of our values.
Then we hash the underlying values. A variant hashes as a pair of values, first the size_t
index of the held type, then the contained value, so we need to accumulate the hashes.
Finally, we verify that the std::variant
hasher produces the same results as the underlying hashers.