Boost logo

Boost :

Subject: Re: [boost] Why no non-null smart pointers?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2014-04-29 05:25:09


On Mon, Apr 28, 2014 at 9:42 PM, Jeff Hill <johill_at_[hidden]> wrote:
> Maybe there /is/ even a backwards compatible solution.
>
> Perhaps an optional null object pattern could be employed in methods, and or
> constructors, of shared_ptr that might establish a raw nill state? Perhaps
> something like this obviously oversimplified example could be use.
>
> template < typename T>
> void shared_ptr < T > :: reset ( T * p = 0 )
> {
> if ( p ) {
> /* what happens in shared_ptr now */
> }
> else {
> /* new behavior in shared_ptr, when it detects a nill raw pointer */
> *this = NullObjectTypes < T > :: factory ();
> }
> }

Please, don't change shared_ptr (or any other *_ptr) behavior. They
are intended to behave as pointers, and pointers do have a null state.
This is a useful feature in many cases.

I'd prefer the functionality you're looking for to be implemented as a
separate component/library. It may be based on pointers internally
though.


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