On Wed, May 27, 2026 at 06:15:04PM -0400, Jason Merrill wrote:
> On 5/27/26 4:32 PM, Marek Polacek wrote:
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > [temp.arg.nontype] tells us that a temporary object is not an acceptable
> > template-argument when the corresponding template parameter has reference
> > type.  So
> > 
> >    template<const int &> struct B {};
> >    B<1> b;
> > 
> > is ill-formed.  In the test below we have a tparm `const int &I` and we
> > are trying to deduce `I` from `A<0>`.  Since a temporary would be required
> > for the template argument, this should be a deduction failure.
> > 
> >     PR c++/107124
> > 
> > gcc/cp/ChangeLog:
> > 
> >     * pt.cc (unify) <case TEMPLATE_PARM_INDEX>: Return
> >     unify_type_mismatch if a temporary would be required for the
> >     template argument.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> >     * g++.dg/template/deduce11.C: New test.
> >     * g++.dg/template/deduce12.C: New test.
> > ---
> >   gcc/cp/pt.cc                             |  7 ++++++-
> >   gcc/testsuite/g++.dg/template/deduce11.C | 15 +++++++++++++++
> >   gcc/testsuite/g++.dg/template/deduce12.C | 11 +++++++++++
> >   3 files changed, 32 insertions(+), 1 deletion(-)
> >   create mode 100644 gcc/testsuite/g++.dg/template/deduce11.C
> >   create mode 100644 gcc/testsuite/g++.dg/template/deduce12.C
> > 
> > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> > index ada5f53a9f1..41b86a4e4f7 100644
> > --- a/gcc/cp/pt.cc
> > +++ b/gcc/cp/pt.cc
> > @@ -26485,7 +26485,12 @@ unify (tree tparms, tree targs, tree parm, tree 
> > arg, int strict,
> >     ;
> >         else if (same_type_ignoring_top_level_qualifiers_p
> >            (non_reference (TREE_TYPE (arg)),
> > -           non_reference (tparm)))
> > +           non_reference (tparm))
> > +          /* A temporary object is not an acceptable template-argument
> > +             when the corresponding template parm has reference type.  */
> > +          && !(TYPE_REF_P (tparm)
> > +               && ref_conv_binds_to_temporary
> > +                  (tparm, convert_from_reference (arg)).is_true ()))
> 
> This seems like an awkward place for this check since it isn't a type
> mismatch, which leads to the unhelpful diagnostic
> 
> >         •   mismatched types ‘const int&’ and ‘int’
> 
> I think better would be to after checking for pack mismatch, check
> invalid_tparm_referent_p, get the diagnostic from there, and return plain
> unify_invalid if it fails.

I put it there because it seemed simple to fall back on unify_type_mismatch
(and it's what clang++ says too).  But fair enough; I agree with your point.

I can't use invalid_tparm_referent_p because it wouldn't detect the problem
(it's the first thing I tried).  So I propose this version.

dg.exp passed so far.

-- >8 --
[temp.arg.nontype] tells us that a temporary object is not an acceptable
template-argument when the corresponding template parameter has reference
type.  So

  template<const int& CRI> struct B { };
  B<1> b;

is ill-formed.  In the test below we have a tparm `const int &I` and we
are trying to deduce `I` from `A<0>`.  Since a temporary would be required
for the template argument, this should be a deduction failure.

        PR c++/107124

gcc/cp/ChangeLog:

        * pt.cc (unify_temporary): New.
        (unify) <case TEMPLATE_PARM_INDEX>: Return unify_temporary
        if a temporary would be required for the template argument.

gcc/testsuite/ChangeLog:

        * g++.dg/template/deduce11.C: New test.
        * g++.dg/template/deduce12.C: New test.
---
 gcc/cp/pt.cc                             | 17 +++++++++++++++++
 gcc/testsuite/g++.dg/template/deduce11.C | 15 +++++++++++++++
 gcc/testsuite/g++.dg/template/deduce12.C | 11 +++++++++++
 3 files changed, 43 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/template/deduce11.C
 create mode 100644 gcc/testsuite/g++.dg/template/deduce12.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index ada5f53a9f1..659c07f9800 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -7369,6 +7369,16 @@ unify_template_argument_mismatch (bool explain_p, tree 
parm, tree arg)
   return unify_invalid (explain_p);
 }
 
+static int
+unify_temporary (bool explain_p, tree parm, tree arg)
+{
+  if (explain_p)
+    inform (input_location,
+           "  template argument %qE for %qT would require a temporary",
+           arg, parm);
+  return unify_invalid (explain_p);
+}
+
 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
    argument for TYPE, points to an unsuitable object.
 
@@ -26530,6 +26540,13 @@ unify (tree tparms, tree targs, tree parm, tree arg, 
int strict,
          && !TEMPLATE_PARM_PARAMETER_PACK (parm))
        return unify_parameter_pack_mismatch (explain_p, parm, arg);
 
+      /* A temporary object is not an acceptable template-argument
+        when the corresponding template parm has reference type.  */
+      if (TYPE_REF_P (tparm)
+         && ref_conv_binds_to_temporary
+            (tparm, convert_from_reference (arg)).is_true ())
+       return unify_temporary (explain_p, tparm, arg);
+
       {
        bool removed_attr = false;
        arg = strip_typedefs_expr (arg, &removed_attr);
diff --git a/gcc/testsuite/g++.dg/template/deduce11.C 
b/gcc/testsuite/g++.dg/template/deduce11.C
new file mode 100644
index 00000000000..c7671c04397
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/deduce11.C
@@ -0,0 +1,15 @@
+// PR c++/107124
+// { dg-do compile { target c++11 } }
+
+template<int>
+struct A {};
+
+template<const int &I>
+constexpr int f (A<I>) { return 0; }
+
+template<typename T>
+constexpr int f (T) { return 1; }
+
+const int a = 42;
+static_assert (f (A<0>{}) == 1, "");
+static_assert (f (A<a>{}) == 1, "");
diff --git a/gcc/testsuite/g++.dg/template/deduce12.C 
b/gcc/testsuite/g++.dg/template/deduce12.C
new file mode 100644
index 00000000000..11ab9e3b865
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/deduce12.C
@@ -0,0 +1,11 @@
+// PR c++/107124
+// { dg-do compile { target c++11 } }
+
+template<int>
+struct A {};
+
+template<const int &I>
+constexpr int f (A<I>) { return 0; }
+
+constexpr int i = f (A<0>{}); // { dg-error "no matching function for call" }
+// { dg-message "(candidate|require a temporary)" "candidate note" { target 
*-*-* } .-1 }

base-commit: b64700dec403f9b2b605df295e43c73b3185d0f5
-- 
2.54.0

Reply via email to