Subject: Re: [boost] [shared_ptr] enable_shared_from_any ?
From: Philip Warner (pjw_at_[hidden])
Date: 2010-05-17 20:46:47
On 18/05/2010 10:33 AM, Frank Mori Hess wrote:
> 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.
Sorry, I did not mean it "does not work as a concept", merely that it
does not achieve the pain-free implementation what works with any
"user-created" shared_ptr, which is what I was trying to achieve:
from my original post:
> Specifically, what I would like to see is a solution to the following code:
> C1 *p = new C1();
> shared_ptr<C1> ptr(p);
> ...some time later....
> shared_ptr<C1> ptr2<p>;
> and the resulting mess because of double-deallocation.
if I have this then I can return raw pointers from covariant functions
then wrap the result in a shared pointer again and it won't matter of
the library also has it wrapped in a shared pointer. The only
requirement will be that they use a compatible version of boost.
> 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.
Indeed, but it does not really solve the problem in the general case
(without discipline and pitfalls).
I am very happy to provide a patch that:
- adds a class, enable_shared_from_any, that can be inherited from
- updates shared_ptr to work in the above code, if C1 inherits from
What we have at the moment is something like 'SafeSharedPtr' that
inherits from 'shared_ptr' which does what I want, but which is less
efficient than a native implementation would be