Boost logo

Boost Users :

From: Boris (boriss_at_[hidden])
Date: 2007-03-21 19:43:31


On Wed, 21 Mar 2007 21:40:54 +0200, JOAQUIN LOPEZ MU?Z <joaquin_at_[hidden]>
wrote:

> [...]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<shared_ptr<foo> >,
>
> which compares pointers, not foo objects. Hence the problem.
> Rather than overloading std::equal_to<shared_ptr<foo> >,
> which breaks the natural equality semantics of shared_ptr,
> my advice is that you change the key of the hashed index
> to:
>
> indexed_by<hashed_unique<identity<foo > > >
>
> 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


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