[Hash] Custom hash_value for Pointers
Dear all, Should not the program in the P.S. output "I got used!"? If not, how can I ensure that it's size_t hash_value(B*) which gets called upon use of bPtrHasher's operator()? TIA, --Hossein P.S. namespace Bs { struct B { virtual string id() const {return "This is a B!";} }; struct D: B { string id() const {return "And, this one is a D.";} }; size_t hash_value(B* bptr) { cout << "I got used!" << endl; boost::hash<string> hasher; return hasher(bptr->id()); } } int main() { Bs::B* bp1 = new Bs::B; Bs::B* bp2 = new Bs::D; boost::hashBs::B* bPtrHasher; size_t h1 = bPtrHasher(bp1), h2 = bPtrHasher(bp2); }
On 27 August 2010 18:53, Hossein Haeri
Dear all,
Should not the program in the P.S. output "I got used!"? If not, how can I ensure that it's size_t hash_value(B*) which gets called upon use of bPtrHasher's operator()?
I don't know, my ADL knowledge is a bit rusty, but you shouldn't be doing that anyway. 'boost::hash' is designed to be used alongside 'std::equal_to'. For pointers it uses the standard pointer equality comparison, so 'boost::hash' should be using the standard hash function to match it. If you want to use a different hash algorithm, than you should use your own hash class, just as if you wanted to for integers. Daniel
participants (2)
-
Daniel James
-
Hossein Haeri