Quick Links: |
Provide hashAppend
for std::variant
.
More...
Namespaces | |
namespace | bslh |
hashAppend
for std::variant
. 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. std::variant
first appears. std::variant<int, double>
for this, and demonstrate that such a value can be correctly hashed. typedef std::variant<int, double> id; id variantInt = 42; id variantDouble = 3.475;
bslh::Hash<> hasher;
size_t variantIntHash = hasher(variantInt); size_t variantDoubleHash = hasher(variantDouble);
size_t
index of the held type, then the contained value, so we need to accumulate the hashes. bslh::Hash<>::HashAlgorithm haInt; hashAppend(haInt, size_t(0)); hashAppend(haInt, 42); size_t expectedIntHash = size_t(haInt.computeHash()); bslh::Hash<>::HashAlgorithm haDouble; hashAppend(haDouble, size_t(1)); hashAppend(haDouble, 3.475); size_t expectedDoubleHash = size_t(haDouble.computeHash());
std::variant
hasher produces the same results as the underlying hashers. assert(expectedIntHash == variantIntHash); assert(expectedDoubleHash == variantDoubleHash);