|
Boost : |
From: Rani Sharoni (rani_sharoni_at_[hidden])
Date: 2003-12-18 13:30:42
Peter Dimov wrote:
> Rani Sharoni wrote:
>
>> Why is it important that the shared_ptr(weak_ptr<Y> const & r)
>> constructor throws an exception?
>
> If a function's requirements are met, but the postcondition cannot,
> this function should throw an exception.
>
> It is not possible to place a !r.expired() requirement on the
> constructor, because the user can never enforce it reliably. The only
> remaining
> shared_ptr in r's ownership group may be destroyed (by another
> thread, for example) between the r.expired() test and the constructor
> call.
>
> If you don't want an exception, use r.lock().
It seems that the lock construct is superior because it encourage the user
to condition the dereferencing. OTOH the throwing constructor is easy to use
and therefore users might mistakenly abuse it. I know that it can be avoided
after realizing what the purpose of weak_ptr is but maybe replacing this
constructor with throwing well-named free function will help to avoid
potential pitfalls.
Thanks,
Rani
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk