From ed7f9957bbb5e899099e1ef0e5f6234c453ef7da Mon Sep 17 00:00:00 2001 From: kamlesh kumar Date: Mon, 2 Nov 2020 20:40:21 +0530 Subject: [PATCH] c++: DR2303, ambiguous base deduction [PR97453] When there are two possible matches and one is a base of the other, choose the derived class rather than fail. gcc/cp/ChangeLog 2020-10-21 Kamlesh Kumar Jason Merrill PR c++/97453 DR2303 * pt.c (get_template_base): Consider closest base in template deduction when base of base also matches. gcc/testsuite/ChangeLog 2020-10-21 Kamlesh Kumar * g++.dg/DRs/dr2303.C: New test. --- gcc/cp/pt.c | 16 +++++++++++-- gcc/testsuite/g++.dg/DRs/dr2303.C | 37 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/DRs/dr2303.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index b782fb535eb..f401c75b9e5 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -22699,8 +22699,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)) + /* Ignore 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{}); +} -- 2.30.2