[Boost-bugs] [Boost C++ Libraries] #9755: Simple way to get hash.

Subject: [Boost-bugs] [Boost C++ Libraries] #9755: Simple way to get hash.
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-03-07 12:25:16


#9755: Simple way to get hash.
--------------------------------------+-------------------------
 Reporter: Rainer Deyke <rainerd@…> | Owner: danieljames
     Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: hash
  Version: Boost 1.55.0 | Severity: Cosmetic
 Keywords: |
--------------------------------------+-------------------------
 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);
   }

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/9755>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:15 UTC