Bug 83 - Smart pointers don't interact well with STL containers
Smart pointers don't interact well with STL containers
Status: RESOLVED FIXED
Product: ns-3
Classification: Unclassified
Component: core
pre-release
PC All
: P1 normal
Assigned To: ns-bugs
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2007-10-05 14:04 UTC by Gustavo J. A. M. Carneiro
Modified: 2007-10-08 11:16 UTC (History)
0 users

See Also:


Attachments
test program (529 bytes, text/x-c++src)
2007-10-05 14:05 UTC, Gustavo J. A. M. Carneiro
Details

Note You need to log in before you can comment on or make changes to this bug.
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 ;-)