Drake
Drake C++ Documentation
hash_append generic hashing

Drake uses the hash_append pattern as described by N3980.

For a full treatment of the hash_append pattern, refer to: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3980.html

Providing hash_append support within a class

Drake types may implement a hash_append function. The function appends every hash-relevant member field into the hasher:

class MyValue {
public:
...
/// Implements the @ref hash_append concept.
template <class HashAlgorithm>
friend void hash_append(
HashAlgorithm& hasher, const MyValue& item) noexcept {
hash_append(hasher, item.my_data_);
}
...
private:
std::string my_data_;
};

Checklist for reviewing a hash_append implementation:

Using hashable types

Types that implement this pattern may be used in unordered collections:

std::unordered_set<MyValue, drake::DefaultHash> foo;

Some Drake types may also choose to specialize std::hash<MyValue> to use DefaultHash, so that the second template argument to std::unordered_set can be omitted. For example, Drake's symbolic::Expression header says:

namespace std {
struct hash<drake::symbolic::Expression> : public drake::DefaultHash {};
} // namespace std

so that users are able to simply write:

std::unordered_set<drake::symbolic::Expression> foo;