Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr design question
From: Peter Dimov (pdimov_at_[hidden])
Date: 2011-09-20 12:38:44


Igor R wrote, about the aliasing constructor:

> Wow! I never payed attention to that ctor. Great feature.
> I think it's worth to be highlighted somewhere in the docs.

This is a relatively late addition (shared_ptr it was already in std:: and
part of the draft standard when the aliasing constructor was added). It's
worth noting that it was always possible to do this:

http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/sp_techniques.html#another_sp

(you just need to realize that the "another smart pointer" can be
shared_ptr) but the aliasing constructor made it more efficient and more
explicit (with respect to the shared ownership).

Getting back to the original question, it's possible to have two
shared_ptr<T> instances that share ownership and have different get()
pointers, without any aliasing tricks:

struct X {};
struct Y: X {};
struct Z: X {};
struct W: Y, Z {};

shared_ptr<W> pw( new W );
shared_ptr<Y> py( pw );
shared_ptr<Z> pz( pw );
shared_ptr<X> px( py );
shared_ptr<X> px2( pz );


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net