Debugger says that friend std::size_t hash_value(Base* const& base) is never called (GCC 4.7). Why is that?

EDIT 3:
I found out that template <class T> std::size_t hash_value(T* const& v) in boost/functional/hash.hpp on line #215 (Boost 1.49) is Boost's specialization for pointers and it simply masks your custom implementation of hash_value such as mine in EDIT 2.
Therefore, it seems like the only way here is to create a custom Hash Functor.

2 Answers
2

For the hash function, you have a choice between specializing boost::hash (or std::hash in the newer standard) or defining a new functor class. These alternatives work equally well.

For the equality operator, you need to define a new functor, because you cannot redefine the equality operator over pointers. It's a built-in operator (defined in functional terms as bool operator==( T const *x, T const *y )) and cannot be replaced.

Both of these can be defined generically by using a templated operator() in a non-templated class.

@Haroogan You overloaded hash_value in your own namespace; you did not specialize the class template hash in namespace boost as I mentioned. The hash_value function is a Boost extension which was not standardized and as such should be avoided. Sorry for the confusion, I've just never heard of hash_value before.
–
PotatoswatterApr 9 '12 at 0:18