How do I combine hash values in C++0x?

Neil G picture Neil G · Apr 7, 2010 · Viewed 35.6k times · Source

C++0x adds hash<...>(...).

I could not find a hash_combine function though, as presented in boost. What is the cleanest way to implement something like this? Perhaps, using C++0x xor_combine?

Answer

Karl von Moor picture Karl von Moor · Apr 7, 2010

Well, just do it like the boost guys did it:

template <class T>
inline void hash_combine(std::size_t& seed, const T& v)
{
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}