Boost logo

Boost Users :

Subject: [Boost-users] Boost.Unordered: custom hasher
From: gast128 (gast128_at_[hidden])
Date: 2011-03-01 11:45:59


Hello all,

for using the unordered_set with a user defined class one can define a custom
hasher. There seems to be the following options:
- overide boost::hash_value
- write ur own custom hash functor

Both options have then an external hash function. I was more thinking about
using a member function as hasher (e.g. like GetHashCode). A supporting class
would be then:

template <typename T, typename RetType, RetType (T::*pmf)() const>
struct unary_function_mf : std::unary_function<T, RetType>
{
   RetType operator()(const T& r) const
   {
     return (r.*pmf)();
   }
};

which makes specifying an unordered_set easier:

boost::unordered_set<Person, unary_function_mf<Person, size_t,
&Person::GetHashCode> > uset2;

My questions:
1) is this correct c++
2) does something similar exist in Boost (Boost.Function and Bind doesn't seem
to support his)
3) is there an advantage of using a free function over functor? The free
function has at least one advantage, that it also works with the std::pair
hasher.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net