Bug 83

Summary: Smart pointers don't interact well with STL containers
Product: ns-3 Reporter: Gustavo J. A. M. Carneiro <gjcarneiro>
Component: coreAssignee: ns-bugs <ns-bugs>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P1    
Version: pre-release   
Hardware: PC   
OS: All   
Attachments: test program

Description Gustavo J. A. M. Carneiro 2007-10-05 14:04:40 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 :-(
Comment 1 Gustavo J. A. M. Carneiro 2007-10-05 14:05:12 UTC
Created attachment 72 [details]
test program
Comment 2 Gustavo J. A. M. Carneiro 2007-10-05 14:10:40 UTC
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);
}

Comment 3 Tom Henderson 2007-10-05 15:22:30 UTC
Looks OK to me.
Comment 4 Mathieu Lacage 2007-10-08 02:39:37 UTC
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);
}

Comment 5 Gustavo J. A. M. Carneiro 2007-10-08 05:55:56 UTC
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 ;-)