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 );

At 12:06 PM +0200 9/17/04, Vukasin Toroman wrote:
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.

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 Friday 2004-09-17 13:05, you wrote:
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"

At 1:40 PM +0300 9/20/04, Peter Dimov wrote:
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