BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslh_hashtuple

Outline

Purpose

Provide hashAppend for std::tuple.

Description

This component provides a free function template, bslh::hashAppend, overloaded for the std::tuple class template. Including this function allows for std::tuple 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 2011 or later, as that is when std::tuple first appears.

Usage

This section illustrates intended usage of this component.

Example 1: Hashing a Tuple of Integer Values

Suppose one must compute has that combines the hashes of several integer values, each of a different type:

char c = 'a';
short s = static_cast<short>(1);
int i = 2;
long l = 3L;

First, we can make that calculation by repeated invocations of a 'bslh::DefaultHashAlogorithm object:

hasherS(&c, sizeof(char));
hasherS(&s, sizeof(short));
hasherS(&i, sizeof(int));
hasherS(&l, sizeof(long));
Definition bslh_defaulthashalgorithm.h:346
InternalHashAlgorithm::result_type result_type
Typedef indicating the value type returned by this algorithm.
Definition bslh_defaulthashalgorithm.h:374
result_type computeHash()
Definition bslh_defaulthashalgorithm.h:428

Now, the same calculation can be expressed more concisely if those same values are contained in a single std::tuple object.

std::tuple<char, short, int, long> t = std::make_tuple(c, s, i, l);
bslh::hashAppend(hasherT, t);
bsl::enable_if<(bsl::is_integral< TYPE >::value||bsl::is_pointer< TYPE >::value||bsl::is_enum< TYPE >::value)&&!bsl::is_same< TYPE, bool >::value >::type hashAppend(HASH_ALGORITHM &hashAlg, TYPE input)
Definition bslh_hash.h:638

Finally, we confirm that we computed the same result.

assert(hashS == hashT);