Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr to object in stl set container?
From: Rob Desbois (rob.desbois_at_[hidden])
Date: 2011-12-13 04:49:32

On 13 December 2011 03:23, Galen <gjwilkerson_at_[hidden]> wrote:

> -- I'm trying to use shared_ptr in a stl set, but am having trouble with
> find()
> Any help? Documentation on this seems hard to find.

I know you are overloading the comparison operators for your particular
shared_ptr, but you're doing so in a way that fundamentally changes what
the comparison means - i.e. instead of NodePtr1 == NodePtr2 comparing
pointers, as is default - and useful - for shared_ptr, you're making it
compare the nodes. I understand why, but wouldn't recommend it.

The ptr_container library excels at exactly this sort of usage, and allows
for natural comparisons without you needing to hijack another type's

Your operators are likely not being called because they are of the
incorrect signature. The type of a member equality operator should be as
struct Foo
   bool operator==(const Foo& f) const;

   // this is the incorrect equivalent of your operator
   // bool operator==(Foo f);

The const parameter is the correct way to do it, but it is most likely the *
method* not being const that prevents your operators from being invoked.

All in all, I'd suggest replace the containers you have with
boost::ptr_set<Node> and your life will be much easier :-)

Rob Desbois
"I disapprove of what you say, but I’ll defend to the death your right to
say it", Voltaire

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at