[PATCH] c++: alias CTAD and copy deduction guide [PR115198]

Patrick Palka ppalka@redhat.com
Thu Jun 13 15:05:44 GMT 2024


On Thu, 23 May 2024, Jason Merrill wrote:

> On 5/23/24 17:42, Patrick Palka wrote:
> > On Thu, 23 May 2024, Jason Merrill wrote:
> > 
> > > On 5/23/24 14:06, Patrick Palka wrote:
> > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
> > > > OK for trunk/14?
> > > > 
> > > > -- >8 --
> > > > 
> > > > Here we're neglecting to update DECL_NAME during the alias CTAD guide
> > > > transformation, which causes copy_guide_p to return false for the
> > > > transformed copy deduction guide since DECL_NAME is still __dguide_C
> > > > with TREE_TYPE C<B, T> but it should be __dguide_A with TREE_TYPE A<T>
> > > > (equivalently C<false, T>).  This ultimately results in ambiguity during
> > > > overload resolution between the copy deduction guide vs copy ctor guide.
> > > > 
> > > > This patch makes us update DECL_NAME of a transformed guide accordingly
> > > > during alias CTAD.  This eventually needs to be done for inherited CTAD
> > > > too, but it's not clear what identifier to use there since it has to be
> > > > unique for each derived/base pair.  For
> > > > 
> > > >     template<bool B, class T> struct A { ... };
> > > >     template<class T> struct B : A<false, T> { using A<false, T>::A; }
> > > > 
> > > > at first glance it'd be reasonable to give inherited guides a name of
> > > > __dguide_B with TREE_TYPE A<false, T>, but since that name is already
> > > > used B's own guides its TREE_TYPE is already B<T>.
> > > 
> > > Why can't it be the same __dguide_B with TREE_TYPE B<T>?
> > 
> > Ah because copy_guide_p relies on TREE_TYPE in order to recognize a copy
> > deduction guide, and with that TREE_TYPE it would still incorrectly
> > return false for an inherited copy deduction guide, e.g.
> > 
> >    A(A<B, T>) -> A<B, T>
> > 
> > gets transformed into
> > 
> >    B(A<false, T>) -> B<T>
> > 
> > and A<false, T> != B<T> so copy_guide_p returns false.
> 
> Hmm, that seems correct; the transformed candidate is not the copy deduction
> guide for B.

By https://eel.is/c++draft/over.match.class.deduct#3.4 it seems that a
class template can now have multiple copy deduction guides with inherited
CTAD: the derived class's own copy guide, along with the transformed copy
guides of its eligible base classes.  Do we want to follow the standard
precisely here, or should we maybe restrict the copy-guideness propagation
to alias CTAD only?

> 
> > But it just occurred to me that this TREE_TYPE clobbering of the
> > __dguide_foo identifier already happens if we have two class templates
> > with the same name in different namespaces, since the identifier
> > contains only the terminal name.  Maybe this suggests that we should
> > use a tree flag to track whether a guide is the copy deduction guide
> > instead of setting TREE_TYPE of DECL_NAME?
> 
> Good point.
> 
> Jason
> 
> 



More information about the Gcc-patches mailing list