Boost logo

Boost :

Subject: Re: [boost] Why no non-null smart pointers?
From: Jonathan Wakely (jwakely.boost_at_[hidden])
Date: 2014-04-29 10:52:40


On 29 April 2014 15:32, Jeff Hill wrote:
> template <>
> shared_ptr < ChessPiece > NullObjectTypes < ChessPiece > :: factory ()
> {
> static ChessPieceNull chessPieceNull;
> return shared_ptr < ChessPiece > ( &chessPieceNull, nullDeleter () );
> }

N.B. This means every shared_ptr that owns the NullObject gets created
with unique ownership, creating a new control block on the heap.

Sometimes that might be what you want, but other times it would be
better to copy an existing object so all null objects of a given type
share ownership:

template <>
shared_ptr < ChessPiece > NullObjectTypes < ChessPiece > :: factory ()
{
    static ChessPieceNull chessPieceNull;
    static shared_ptr < ChessPiece > nullsp( &chessPieceNull, nullDeleter () );
    return nullsp;
}

(This comment shouldn't be seen as endorsement of the proposal ;-)


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