Boost logo

Boost :

Subject: Re: [boost] [SmartPtr] alternative to enable_shared_from_this
From: Roberto Hinz (robhz786_at_[hidden])
Date: 2017-01-05 13:02:21


On Thu, Jan 5, 2017 at 1:13 PM, Boris Rasin <boris_at_[hidden]> wrote:

> Here is how I fixed multiple inheritance problem:
>
> template <typename T>
> class virtual_shared_from_this : public virtual
> std::enable_shared_from_this<void>
> {
> public:
> std::shared_ptr<T> shared_from_this()
> {
> return std::shared_ptr<T>(std::enable_shared_from_this<void>::shared_from_this(),
> static_cast<T*>(this));
> }
> };
>
> You use virtual_shared_from_this exactly as you would use
> enable_shared_from_this. It supports multiple inheritance and is very
> efficient (no use of dynamic_cast).

This indeed solves the problem of multiple inheritance. But I think I
didn't expose well enough the other problem, which has to do with cyclic
references:

Suppose an object A has a shared_ptr to an object B which has a weak_ptr to
A. Every time this weak_ptr is used, it may not be eligible anymore to
originate a non null shared_ptr. In this case, if B can nicely handle this
locally ( checking the result of weak_ptr<A_type>::lock() ), then fine. But
if it can't, is has to throw an exception. And this can be highly
undesirable. One way to solve this problem would turn B a member object of
A, or manage B with a unique_ptr owned by A. This way we ensure that A is
never destroyed before B. But suppose that for some reason, there is
something that requires a shared_ptr to B. How do you solve that?

I have to agree that this is a very specific situation. But when it
happens, it seems that it can only be solved with shared_handle ( or with
enable_shared_from_raw which has no doc and I didn't know exists until now
).


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