Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2008-05-02 08:18:19


Frank Mori Hess:
...
>> > 1. Allowing only (expired) weak_ptr instances to be created in the
>> > constructor;
>> > 2. Bringing the weak_ptr instances back to life once a shared_ptr
>> > takes ownership.
>> >
>> > This however seems to require a relatively major surgery to
>> > sp_counted_base and friends; I'm not sure I'm comfortable with that.
>>
>> It sounds like you're heading in the direction of the "shared_from_that"
>> idea I floated earlier:
>>
>> http://lists.boost.org/Archives/boost/2008/04/135372.php
>>
>> which would allow client code to distinguish between the cases of
>> "shared_ptr not available yet" and "shared_ptr expired".
>
> To be clear what I'm talking about, I've implemented a little proof of
> concept of "shared_from_that" and "enable_shared_from_that", and a little
> test program.

What I had in mind is illustrated with the following:

X * px = new X;
// px->shared_from_this(); // throws bad_weak_ptr
weak_ptr<X> wx = px->weak_from_this(); // OK
assert( wx.use_count() == 0 ); // expired
shared_ptr<X> sx( px );
assert( wx.use_count() == 1 ); // shares ownership with sx

Also, not really related, but food for thought: the old behavior allows me
to do something like the following pseudocode:

void load( Archive & ar, shared_ptr<X> & px )
{
    X * p;
    ar >> p;

    try
    {
        px = p->shared_from_this();
    }
    catch( bad_weak_ptr )
    {
        px.reset( p );
    }
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk