Boost logo

Boost Users :

From: Jason Winnebeck (yg-boost-users_at_[hidden])
Date: 2003-07-21 22:11:42


Is there a reason why weak_ptr's cannot be compared with ==, or tested
in a conditional?

If I have two weak_ptr's, I thought it to be strange that I have to do:

(w1.lock() == w2.lock())

And to test if a weak_ptr is the empty weak_ptr:

if ( !(w1.lock()) )

which seems kind of awkward.

Looking at the implementation, I see how the copy constructor is defined
using lock. I understand what the comments are saying there. Perhaps
there are some multithreaded issues that prevent equality testing I
don't see.

I would like the semantics that w1 and w2 compare to be equal whether or
not the object they are pointing to are valid or not. I'm not sure if
weak_ptrs become "empty" when the object they point to is destroyed
(because there's no way to test for "emptyness" or "nullness" of a
weak_ptr), but if they do I can see how the equality test can't work,
but not the conditional test.

Even so, why is there not an operator == provided that locks both weak_ptr?

Is it acceptable (the documentation didn't make it clear to me), to use

if ( w1.expired() )

to test if simply if w1 is "null" or "reset".

What I am trying to do is have a "current" pointer that takes the value
of something I want to be set (or note the fact that nothing is set),
but I don't care if the object dies while that pointer is set -- in fact
I don't want to keep it alive because I don't have an easy way to call
reset on current were I to make it a shared_ptr.

Jason


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net