Boost logo

Boost :

Subject: Re: [boost] [SmartPtr] alternative to enable_shared_from_this
From: Boris Rasin (boris_at_[hidden])
Date: 2017-01-05 10:13:51


On 2017-01-03 11:39 PM, Roberto Hinz wrote:
> Dear Boost Community,
>
> I've been following this list for a time with the desire of contributing to
> Boost. It seems to me a good idea to start with small and easy to review
> proposals, before attempting anything big.
>
> So my first shot is shared_handle class, an alternative to
> enable_shared_from_this that doesn't suffer from multiple inheritance
> issues. It also provides the capability of instantiating a shared_ptr to a
> member object.
>
> doc: https://robhz786.github.io/smart_ptr/shared_handle.html
> source: https://github.com/boostorg/smart_ptr/compare/develop...
> robhz786:develop
>
> Unfortunately, the implementation produces at least one regression: clang
> 3.1 crashes when it compiles cpp11_pointer_cast_test.cpp with -std=c++11.
> There may be others since I couldn't test in all compilers used in the
> regression tests.
>
> Nevertheless, is there any interest?
>
> Kind regards,
> Roberto Hinz

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).


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