Addition for shared_ptr

I'd like to propose adding a template function to shared_ptr to allow the automatic testing of a shared_ptr to its raw ptr. The template is as simple as template <class T> bool operator==(boost::shared_ptr<T> const & a, T * const & b) { return a.get() == b; } Here is the case where I need it. I have a vector<Foo> myFoo. In a method of Foo I need to see if this instance is in the vector. I want to do this vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), this );

On Thursday 16 September 2004 22:34, Michael Rutman wrote:
Here is the case where I need it.
I have a vector<Foo> myFoo.
In a method of Foo I need to see if this instance is in the vector. I want to do this
vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), this );
wouldn't it be better to simply specify a compare functor for your container?

At 12:06 PM +0200 9/17/04, Vukasin Toroman wrote:
On Thursday 16 September 2004 22:34, Michael Rutman wrote:
Here is the case where I need it.
I have a vector<Foo> myFoo.
In a method of Foo I need to see if this instance is in the vector. I want to do this
vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), this );
wouldn't it be better to simply specify a compare functor for your container?
I do not believe so. By that logic a compare functor would have been better than all the other template comparisons already approved. Just as I want to be able to type safely compare two shared_ptr's of different types (subclasses for example) I want to type safely compare the shared_ptr to the raw pointer. As an aside, this came about because the initial reaction was to do the following, which is REALLY bad vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), shard_ptr(this) ); It compiles just fine, then quickly crashes when it leaves scope and deletes this.

As an aside, this came about because the initial reaction was to do the following, which is REALLY bad
vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), shard_ptr(this) );
It compiles just fine, then quickly crashes when it leaves scope and deletes this.
having a shared_ptr of *this* is a unrelated issue (in my view a big no-no). you can find some info here: http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html

At 9:18 PM +0200 9/17/04, Vukasin Toroman wrote:
As an aside, this came about because the initial reaction was to do the following, which is REALLY bad
vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), shard_ptr(this) );
It compiles just fine, then quickly crashes when it leaves scope and deletes this.
having a shared_ptr of *this* is a unrelated issue (in my view a big no-no). you can find some info here:
http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
I don't want a shared_ptr to this because it is a big no-no as you point out. What I want is the ability to automatically compare the shared_ptr<T> to a T *.

At Friday 2004-09-17 13:05, you wrote:
At 9:18 PM +0200 9/17/04, Vukasin Toroman wrote:
As an aside, this came about because the initial reaction was to do the following, which is REALLY bad
vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), shard_ptr(this) );
It compiles just fine, then quickly crashes when it leaves scope and deletes this.
having a shared_ptr of *this* is a unrelated issue (in my view a big no-no). you can find some info here:
http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
I don't want a shared_ptr to this because it is a big no-no as you point out. What I want is the ability to automatically compare the shared_ptr<T> to a T *.
I believe a "T const*" would be more appropriate
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"

Michael Rutman wrote:
I'd like to propose adding a template function to shared_ptr to allow the automatic testing of a shared_ptr to its raw ptr.
The template is as simple as
template <class T> bool operator==(boost::shared_ptr<T> const & a, T * const & b) { return a.get() == b; }
Here is the case where I need it.
I have a vector<Foo> myFoo.
In a method of Foo I need to see if this instance is in the vector. I want to do this
vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), this );
This is a reasonable suggestion. It turns out that std::find _is_ specified in a way that allows heterogeneous comparisons, although this may be an oversight on the part of the committee and not a design decision. ;-)

At 1:40 PM +0300 9/20/04, Peter Dimov wrote:
Michael Rutman wrote:
I'd like to propose adding a template function to shared_ptr to allow the automatic testing of a shared_ptr to its raw ptr.
The template is as simple as
template <class T> bool operator==(boost::shared_ptr<T> const & a, T * const & b) { return a.get() == b; }
Here is the case where I need it.
I have a vector<Foo> myFoo.
In a method of Foo I need to see if this instance is in the vector. I want to do this
vector<Foo>::iterator iter = find( myFoo.begin(), myFoo.end(), this );
This is a reasonable suggestion. It turns out that std::find _is_ specified in a way that allows heterogeneous comparisons, although this may be an oversight on the part of the committee and not a design decision. ;-)
I just found out that on gcc 2.96 it also needs the following template <class T> bool operator!=(boost::shared_ptr<T> const & a, T * const & b) { return a.get() != b; } gcc 3.3 works fine without this template but the earlier gcc needs both. I'm not sure how that effects the proposal. Peter, I see that you are the maintainer of shared_ptr. Is there any chance you'll add this for the next release of boost? Thanks
participants (4)
-
Michael Rutman
-
Peter Dimov
-
Victor A. Wagner Jr.
-
Vukasin Toroman