On Wed, 21 Mar 2007 21:40:54 +0200, JOAQUIN LOPEZ MU?Z
[...]Hi again, I answered too fast, the answer above is more or less correct but there's more to it: you have overloaded hash_value for shared_ptr<foo>s so as to use the pointed to foo objects:
std::size_t hash_value(const shared_ptr<foo> &f) { std::size_t seed = 0; boost::hash_combine(seed, f->i); boost::hash_combine(seed, f->j); return seed; }
So far so good; but the hashed index depends not only on the hash functor, but also on an equality predicate, which is by default (in this case)
std::equal_to
, which compares pointers, not foo objects. Hence the problem. Rather than overloading std::equal_to
, which breaks the natural equality semantics of shared_ptr, my advice is that you change the key of the hashed index to: indexed_by
> as suggested in my previous post, and write the overload of hash_value for foo, not for shared_ptr<foo>:
std::size_t hash_value(const foo &f) { std::size_t seed = 0; boost::hash_combine(seed, f.i); boost::hash_combine(seed, f.j); return seed; }
Does this work? Please report back,
Thanks, it worked. I also had to define the comparison operator == for foo though. Boris