absl::HashState

A type-erased version of the hash state concept.

Synopsis

Declared in <absl/hash/hash.h>

class HashState
    : public hash_internal::HashStateBase<HashState>

Description

A type erased version of the hash state concept, for use in user-defined AbslHashValue implementations that can't use templates (such as PImpl classes, virtual functions, etc.). The type erasure adds overhead so it should be avoided unless necessary.

Note: This wrapper will only erase calls to combine_contiguous(H, const unsigned char*, size_t) RunCombineUnordered(H, CombinerF)

All other calls will be handled internally and will not invoke overloads provided by the wrapped class.

Users of this class should still define a template AbslHashValue function, but can use absl::HashState::Create(&state) to erase the type of the hash state and dispatch to their private hashing logic.

This state can be used like any other hash state. In particular, you can call HashState::combine() and HashState::combine_contiguous() on it.

Example:

class Interface { public: template <typename H> friend H AbslHashValue(H state, const Interface& value) { state = H::combine(std::move(state), std::type_index(typeid(*this))); value.HashValue(absl::HashState::Create(&state)); return state; } private: virtual void HashValue(absl::HashState state) const = 0; };

class Impl : Interface { private: void HashValue(absl::HashState state) const override { absl::HashState::combine(std::move(state), v1_, v2_); } int v1_; std::string v2_; };

Base Classes

NameDescription
hash_internal::HashStateBase<HashState>

Type Aliases

Name
AbslInternalPiecewiseCombiner
is_hashable

Member Functions

NameDescription
HashState [constructor]Constructors
operator= Assignment operators

Static Member Functions

NameDescription
Create Create a new HashState instance that wraps state.
combine
combine_contiguous Combine a contiguous array of elements into a hash state.
combine_unordered
combine_weakly_mixed_integer Combine a weakly-mixed integer into a hash state.

Using Declarations

NameDescription
combine Combine an arbitrary number of values into a hash state.
combine_contiguous Combine a contiguous range of elements into a hash state.

Friends

NameDescription
absl::hash_internal::CombineRaw
absl::hash_internal::HashStateBase