Bugzilla – Bug 83
Smart pointers don't interact well with STL containers
Last modified: 2007-10-08 11:16:22 UTC
std::set< Ptr<Something> > does not work very well. I insert different pointers into a set, but in the end the set size is 1. I think the Ptr template class needs to define some more operators, like operator < (), in order to allow correct usage in STL containers. This problem is bad especially because I get no compilation error, only subtly incorrect runtime behaviour :-(
Created attachment 72 [details] test program
OK, it really was operator < missing. OK to commit this? template <typename T> bool operator < (Ptr<T> const &lhs, Ptr<T> const &rhs) { return PeekPointer<T> (lhs) < PeekPointer<T> (rhs); }
Looks OK to me.
Looks good to me although we have agreed to use the "const Foo" syntax rather than the "Foo const" syntax, so, the operator declaration should look like this: template <typename T> bool operator < (const Ptr<T> &lhs, const Ptr<T> &rhs) { return PeekPointer<T> (lhs) < PeekPointer<T> (rhs); }
It is Foo const because of copy-pasting similar code already in ptr.h :-) I prefer const Foo as well; highlights better in gnu emacs ;-)