Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] enable_shared_from_any ?
From: Frank Mori Hess (fmhess_at_[hidden])
Date: 2010-05-17 20:33:26


On Monday 17 May 2010, Philip Warner wrote:
> Among other things, yes. But the bulk of my message was effectively
> suggesting replacing enable_shared_from_this{2} with
> enable_shared_from_any to allow safe creation of shared_ptrs from any
> pointer to an object that inherits from enable_shared_from_any.
>
> I have not tested your specific example, but it looks a lot like one of
> the many attempts we have made in the past to return covariant pointers.
> AFAICT, for good reasons, it is just not possible.

The idea does work. I've used it before and it provides the same
functionality as covariant return types, although it's obviously more of a
pain to implement. There are other variants of it:

http://lists.boost.org/boost-users/2003/02/2996.php

When I said it was untested, I only meant the specific illustrative code I
put in the email was untested and may not even compile.

It seems like you could use weak_from_raw() to get the behavior you want,
by testing if the returned weak_ptr is expired or not, to decide if you
should pass ownership of the object to a new shared_ptr.




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