tags:

views:

149

answers:

1

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?

+4  A: 

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);
}
Polybos
yeah, that's the best I could do too. I don't understand how the standards committee declined something so obvious.
Neil G
@Neil: I agree. I think a simple solution for them would be the requirement of the library to have a hash for `std::pair` (or `tuple`, even). It would compute the hash of each element, then combine them. (And in the spirit of the standard library, in an implementation defined way.)
GMan
There are a lot of obvious things omitted from the standard. The process of intensive peer review makes it difficult to get those little things out the door.