commit 6b0fc692d57f38ec69ea739117ac3d4552cd0d23 Author: kamlesh kumar Date: Mon Nov 2 20:40:21 2020 +0530 c++: Implement DR2303 [PR97453] gcc/cp/ChangeLog 2020-10-21 Kamlesh Kumar Jason Merrill PR c++/97453 DR2303 * pt.c (get_template_base): Implement DR2303, Consider closest base while template deduction when base of base also matches. gcc/testsuite/ChangeLog 2020-10-21 Kamlesh Kumar g++.dg/DRs/dr2303.C: New test. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index f31a1a70473..245b7a83a92 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -22693,8 +22693,20 @@ get_template_base (tree tparms, tree targs, tree parm, tree arg, applies. */ if (rval && !same_type_p (r, rval)) { - *result = NULL_TREE; - return tbr_ambiguous_baseclass; + /* [temp.deduct.call]/4.3: If there is a class C that is a + (direct or indirect) base class of D and derived (directly or + indirectly) from a class B and that would be a valid deduced + A, the deduced A cannot be B or pointer to B, respectively. */ + if (DERIVED_FROM_P (r, rval)) + /* Ignore r. */ + continue; + else if (DERIVED_FROM_P (rval, r)) + /* Discard rval. */; + else + { + *result = NULL_TREE; + return tbr_ambiguous_baseclass; + } } rval = r; diff --git a/gcc/testsuite/g++.dg/DRs/dr2303.C b/gcc/testsuite/g++.dg/DRs/dr2303.C new file mode 100644 index 00000000000..b6acb6e2197 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2303.C @@ -0,0 +1,37 @@ +// DR 2303 +// PR c++/97453 +// { dg-do compile { target c++11 } } + +template struct A; +template <> struct A<> +{ +}; +template struct A : A +{ +}; +struct B : A +{ +}; + +struct C : A, A // { dg-warning "direct base .A. inaccessible in .C. due to ambiguity" } +{ +}; + +struct D : A, A // { dg-warning "direct base .A. inaccessible in .D. due to ambiguity" } +{ +}; +template +void +f (const A &) +{ + static_assert (sizeof...(T) == 2, "it should duduce to A"); +} + + +void +g () +{ + f (B{}); + f (C{}); + f (D{}); +}