Hello, I have: std::set< boost::shared_ptr< A > > m_set; and when I try to do: m_set.erase( somesharedptr ); the shared_ptr is not removed from the set. There is already a shared_ptr object pointing to the same A object inside the set. I think it's because the definition of "shared_ptr<>::operator<" and its use in std::set<>::erase. What's the correct way of using a shared_ptr inside a set? Thanks.
Javier Jerónimo wrote:
Hello,
I have:
std::set< boost::shared_ptr< A > > m_set;
and when I try to do:
m_set.erase( somesharedptr );
the shared_ptr is not removed from the set. There is already a shared_ptr object pointing to the same A object inside the set. I think it's because the definition of "shared_ptr<>::operator<" and its use in std::set<>::erase.
What's the correct way of using a shared_ptr inside a set? You will need to provide a less than predicate to the std::set that looks through the shared pointer to its pointee value. You need that anyway if you want the set to be a set of pointee values rather than a set of shared pointers.
The less than operator for shared_ptr compares the address of the pointee, not its value.
Thanks.
Bill Somerville
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 09 October 2008 10:58 am, Bill Somerville wrote:
The less than operator for shared_ptr compares the address of the pointee, not its value.
No, I believe it provides a partial weak ordering based on shared ownership. It's possible for two shared_ptr that share ownership to point at different addresses (via the aliasing constructor for example). -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFI7iZ15vihyNWuA4URAvWlAKCDP42K5Ml2sfH7cIprJWYj/7k3sgCeJ9dq tvWkCTEvoDWi8gEPDsj0ou0= =HJ+E -----END PGP SIGNATURE-----
participants (3)
-
Bill Somerville
-
Frank Mori Hess
-
Javier Jerónimo