Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-10-16 08:10:27


John Torjo wrote:
> Peter Dimov wrote:
>
>> John Torjo wrote:
>>
>>> Hi Peter,
>>>
>>> Just a thought: perhaps this constructor:
>>> explicit shared_ptr(weak_ptr<Y> const & r);
>>>
>>> should be private,
>>> and the only way to create a shared_ptr from a weak_ptr is:
>>>
>>> shared_ptr<T> val = weak->lock();
>>>
>>> This way, the shared_ptr public constructors will never throw. Users
>>> will not make mistakes like this:
>>>
>>> shared_ptr<T> val = weak;
>>
>>
>> This shouldn't compile on a reasonably compliant compiler, because
>> the constructor is explicit.
>
> True, sorry, I meant this:
>
> shared_ptr<T> val(weak);
>
> The point still remains.

I'm not sure that it does. The canonical way to work with a weak_ptr is

if( shared_ptr<T> p = weak.lock() )
{
    // do things with *p
}

You can't accidentally omit the lock(), and shared_ptr<T> p( weak ) is
invalid in an if statement.

shared_ptr<T> val( weak ); does exactly what it is intended to do.


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