In a C++ program with Boost, I am trying to build an unordered map whose keys are tuples of doubles:
typedef boost::tuples::tuple
Actually, you could perfectly define a general hash function for boost::tuple
. The only requirement is that it lives within the same namespace so that it is picked up by ADL.
I am actually surprised that they did not already write one.
namespace boost { namespace tuples {
namespace detail {
template ::value - 1>
struct HashValueImpl
{
static void apply(size_t& seed, Tuple const& tuple)
{
HashValueImpl::apply(seed, tuple);
boost::hash_combine(seed, tuple.get());
}
};
template
struct HashValueImpl
{
static void apply(size_t& seed, Tuple const& tuple)
{
boost::hash_combine(seed, tuple.get<0>());
}
};
} // namespace detail
template
size_t hash_value(Tuple const& tuple)
{
size_t seed = 0;
detail::HashValueImpl::apply(seed, tuple);
return seed;
}
} }
Note: I have only proved it correct, I haven't tested it.