Boost logo

Boost :

Subject: Re: [boost] Rationale for shared_ptr/array including both ptr to control and ptr to data in class
From: Peter Dimov (pdimov_at_[hidden])
Date: 2011-09-23 10:18:38

Sebastian Redl wrote:
> On 23.09.2011 13:43, Olaf van der Spek wrote:
> > Hi,
> >
> > I was wondering why shared_ptr/array include both a ptr to the control
> > block and a ptr to the data (ptr) in the class itself. I assume it's
> > for performance reasons, but I didn't find anything about that in the
> > docs.
> >
> It's for aliasing. You can have a shared_ptr that points to a subobject of
> the object controlled by the control block.

It's true that the separate pointer enables aliasing, but this is not the
original reason for its existence. It supports pointer conversions
(shared_ptr<T> converts to shared_ptr<U> when T* converts to U*.)

shared_array doesn't technically need aliasing or conversions, its
implementation just mirrors shared_ptr.

Boost list run by bdawes at, gregod at, cpdaniel at, john at