On 16 May 2017 at 13:42, Paul "TBBle" Hampson <
paul.h...@pobox.com> wrote:
> On Tuesday, 16 May 2017 19:39:53 UTC+10, Ville Voutilainen wrote:
>>
>> On 16 May 2017 at 12:13, Paul "TBBle" Hampson <
paul.h...@pobox.com> wrote:
>>
>> > The use-case I've bounced off this from is building an Entity-Component
>> > system. The Entities are shared_ptr, and own unique_ptr to their
>> > components.
>> > We want to be able to expose a method to get an aliased shared_ptr to a
>> > Component from the Entity, using the Entity's ref-count, without making
>> > the
>> > Entity publicly support enable_shared_from_this.
>>
>> That's what
>> template< class Y >
>> shared_ptr( const shared_ptr<Y>& r, element_type* ptr ) noexcept;
>> is for. See
>>
>>
https://wandbox.org/permlink/Vs8D3hLASRaYAryV
>
>
> That's exactly what we've done. And making
> enable_shared_from_this<Component> be privately inherited breaks it, because
> shared_from_this() now throws bad_weak_ptr.
Right, that constructor allows Component to return shared_ptr<Entity>
without Entity publicly supporting
enable_shared_from_this.
> That's the problem we're trying to solve, that brought me to this thread.
Okay; the problem indeed, in general, is that you can't know what
exact part of the library implementation
converts Component to enable_shared_from_this<Component>. So you can't
befriend that part, so you
must use public inheritance.
To provide a better mechanism to use enable_shared_from_this with
non-public inheritance requires
a proposal. I don't think it's a simple patch-fix. I do feel sympathy
for users wanting to do that, I do that
again and again whenever I use enable_shared_from_this, because public
inheritance is not something
I do by default. Then I realize, again, that I can't do private
inheritance in this case. I might be able to
cope with a conversion operator, but I don't want that to be public either.