[PATCH] Make std::enable_shared_from_this cope with ambiguity

Jonathan Wakely jwakely@redhat.com
Wed Oct 19 20:14:00 GMT 2016


On 19/10/16 21:13 +0100, Jonathan Wakely wrote:
>This patch does three things:
>
>1. Refactor std::enable_shared_from_this support code.
>
> Instead of several overloads of __enable_shared_from_this_helper
> that contain the same code but operating on slightly different types
> I've split it into two parts. Upcasting to an accessible+unambiguous
> enable_shared_from_this base class is done by new functions found by
> ADL, __enable_shared_from_this_base. That is called by a new
> template function __shared_ptr::_M_enable_shared_from_this_with()
> which actually does the enabling by calling _M_weak_assign.
>
> Calls to _M_enable_shared_from_this_with(p) closely match the
> wording from my https://wg21.link/p0033r1 paper ("enables
> shared_from_this with p") and are constrained so they don't give an
> error if the __enable_shared_from_this_base() call is ambiguous. We
> already gracefully handled ambiguous std::enable_shared_from_this
> bases (PR56383) but failed noisily if a type had a combination of
> std::enable_shared_from_this, std::__enable_shared_from_this and
> std::experimental::enable_shared_from_this bases. Now we treat those
> combinations gracefully.
>
>2. Change std::experimental::enable_shared_from_this bases to be
> initialized independently of std::enable_shared_from_this bases.
> It's now possible to have both, and for both to be enabled. See
> enable_shared_from_this.cc which tests this.
>
> The standard says we have to enable shared_from_this for types with
> an accessible and unambiguous std::enable_shared_from_this base
> class, and we should be able to do that even if the class also has
> an experimental::enable_shared_from_this base class. Now we can.
>
> Potentially we could do the same for std::__enable_shared_from_this,
> but I'm happy for those bases to be considered ambiguous with
> std::enable_shared_from_this.
>
>3. Don't enable shared_from_this for arrays stored in
> experimental::shared_ptr.  This matches the boost::shared_ptr
> semantics, and I intend to propose this as a fix for C++17 too. It
> doesn't make sense to treat the first element of an array specially
> and enable shared_from_this for the first element only.

I forgot to say that 2. and 3. are appropriate for gcc-6-branch too,
as they only touch the experimental TS code.




More information about the Libstdc++ mailing list