Opened 9 years ago

#9755 new Feature Requests

Simple way to get hash.

Reported by: Rainer Deyke <rainerd@…> Owned by: Daniel James
Milestone: To Be Determined Component: hash
Version: Boost 1.55.0 Severity: Cosmetic
Keywords: Cc:

Description

When writing custom hash_value functions, it is often useful to forward to the hash of another value. However, there is currently no short and simple way to do this. Consider:

template<class T> struct C {

some_complex_mpl_expression<T>::type value; friend std::size_t hash_value() {

First attempt: requires knowledge of the type of the value to hash. return boost::hash<some_complex_mpl_expression<T>::type>()(this->value);

Second attempt: two lines, not recommended. using namespace boost; return hash_value(this->value);

Third attempt: works, but requires three lines of code and doesn't yield the same hash as the first two attempts. std::size_t seed = 0; boost::hash_combine(seed, this->value); return seed;

}

};

I propose the addition of a simple function to calculate the hash of any arbitrary value, defined below:

template<class T> std::size_t get_hash(T const& v) {

return hash<T>()(v);

}

This would allow me to write the above hash_value function with a single line of simple and correct code:

friend std::size_t hash_value() {

return boost::get_hash(this->value);

}

Change History (0)

Note: See TracTickets for help on using tickets.