Subject: Re: [boost] Why no non-null smart pointers?
From: Rob Stewart (robertstewart_at_[hidden])
Date: 2014-04-29 05:22:27
On April 28, 2014 11:20:35 AM EDT, Jeff Hill <johill_at_[hidden]> wrote:
>Ross MacGregor wrote
>> "Johan RÃ¥de"
>> I know it is an insane idea to suggest changing the entire
>> boost::smart_ptr library to a boost::smart_ref library, but I
>> think it may actually be the right thing to do. Does anyone
>I also find the nill state of shared_ptr to be a liability when
>interfaces. The primary purpose of shared_ptr when used in an
>context is to convey ownership, and almost always it feels wrong to
>provide simultaneously an option to convey ownership of nothing. The
>user is left with the responsibility of testing every shared_ptr he has
>received for null state before using it, but its easy to forget and the
>compiler provides no assistance.
You've overlooked the use case of reclaiming ownership from smart_ptr. There's nothing left for it to own after that. (You can't replace it with a default constructed instance, as there may be no default constructor. Any other value you choose would be a sentinel value and you'd want to detect that in many contexts, so checking for null is more straightforward.)
OTOH, you could justify a new class that has no reset() and that rejects null pointers, so it is never null, yet has shared ownership semantics.
(Sent from my portable computation engine)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk