]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: reference tparm refers to temporary object [PR107124]
authorMarek Polacek <polacek@redhat.com>
Wed, 27 May 2026 17:23:49 +0000 (13:23 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 3 Jun 2026 19:01:57 +0000 (15:01 -0400)
[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:

* cp-tree.h: Adjust comments to mention
maybe_build_nontype_implicit_conv instead of
maybe_convert_nontype_argument.
* pt.cc (maybe_convert_nontype_argument): Rename to...
(maybe_build_nontype_implicit_conv): ...this.
(convert_nontype_argument_maybe_dependent): New, factored
out of...
(convert_template_argument): ...here.
(unify) <case TEMPLATE_PARM_INDEX>: Call
convert_nontype_argument_maybe_dependent and return unify_invalid
if it failed.  Don't call strip_typedefs_expr.

gcc/testsuite/ChangeLog:

* g++.dg/template/deduce11.C: New test.
* g++.dg/template/deduce12.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/cp-tree.h
gcc/cp/pt.cc
gcc/testsuite/g++.dg/template/deduce11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/deduce12.C [new file with mode: 0644]

index 65f112714d14093ef2ebab9cbe5660b08d018709..bf477a67a349013c90117f9eb78b2cf1c79288bc 100644 (file)
@@ -5138,7 +5138,7 @@ get_vec_init_expr (tree t)
   (TREE_LANG_FLAG_0 (IMPLICIT_CONV_EXPR_CHECK (NODE)))
 
 /* True if NODE represents a dependent conversion of a non-type template
-   argument.  Set by maybe_convert_nontype_argument.  */
+   argument.  Set by maybe_build_nontype_implicit_conv.  */
 #define IMPLICIT_CONV_EXPR_NONTYPE_ARG(NODE) \
   (TREE_LANG_FLAG_1 (IMPLICIT_CONV_EXPR_CHECK (NODE)))
 
@@ -5148,7 +5148,7 @@ get_vec_init_expr (tree t)
   (TREE_LANG_FLAG_2 (IMPLICIT_CONV_EXPR_CHECK (NODE)))
 
 /* True if NODE represents a conversion forced to be represented in
-   maybe_convert_nontype_argument, i.e. for an alias template.  */
+   maybe_build_nontype_implicit_conv, i.e. for an alias template.  */
 #define IMPLICIT_CONV_EXPR_FORCED(NODE) \
   (TREE_LANG_FLAG_3 (IMPLICIT_CONV_EXPR_CHECK (NODE)))
 
index b334882de818290e02a01214d98c49200860c5ac..4683b57f34b63ec23c62bb42ab61bc7fac8d9276 100644 (file)
@@ -8740,7 +8740,7 @@ is_compatible_template_arg (tree parm, tree arg, tree args)
    conversion for the benefit of cp_tree_equal.  */
 
 static tree
-maybe_convert_nontype_argument (tree type, tree arg, bool force)
+maybe_build_nontype_implicit_conv (tree type, tree arg, bool force)
 {
   /* Auto parms get no conversion.  */
   if (type_uses_auto (type))
@@ -8780,6 +8780,33 @@ dependent_implicit_conv_p (tree type, tree expr, bool forced)
              && value_dependent_expression_p (expr)));
 }
 
+/* Convert the non-type template parameter ARG to the indicated TYPE.
+   If one of them is dependent, create an appropriate conversion.
+   FORCE_CONV is true in a forced context (i.e. alias or concept).  */
+
+static tree
+convert_nontype_argument_maybe_dependent (tree type, tree arg,
+                                         bool force_conv,
+                                         tsubst_flags_t complain)
+{
+  if (dependent_implicit_conv_p (type, arg, force_conv))
+    {
+      tree val = canonicalize_expr_argument (arg, complain);
+      return maybe_build_nontype_implicit_conv (type, val, force_conv);
+    }
+
+  /* We used to call digest_init here.  However, digest_init will report
+     errors, which we don't want when complain is zero.  More importantly,
+     digest_init will try too hard to convert things: for example,
+     `0' should not be converted to pointer type at this point according to
+     the standard.  Accepting this is not merely an extension, since
+     deciding whether or not these conversions can occur is part of
+     determining which function template to call, or whether a given
+     explicit argument specification is valid.  */
+  return convert_nontype_argument (type, convert_from_reference (arg),
+                                  complain);
+}
+
 /* Convert the indicated template ARG as necessary to match the
    indicated template PARM.  Returns the converted ARG, or
    error_mark_node if the conversion was unsuccessful.  Error and
@@ -9060,23 +9087,8 @@ convert_template_argument (tree parm,
          && same_type_p (TREE_TYPE (orig_arg), t))
        orig_arg = TREE_OPERAND (orig_arg, 0);
 
-      if (!dependent_implicit_conv_p (t, orig_arg, force_conv))
-       /* We used to call digest_init here.  However, digest_init
-          will report errors, which we don't want when complain
-          is zero.  More importantly, digest_init will try too
-          hard to convert things: for example, `0' should not be
-          converted to pointer type at this point according to
-          the standard.  Accepting this is not merely an
-          extension, since deciding whether or not these
-          conversions can occur is part of determining which
-          function template to call, or whether a given explicit
-          argument specification is valid.  */
-       val = convert_nontype_argument (t, orig_arg, complain);
-      else
-       {
-         val = canonicalize_expr_argument (orig_arg, complain);
-         val = maybe_convert_nontype_argument (t, val, force_conv);
-       }
+      val = convert_nontype_argument_maybe_dependent (t, orig_arg, force_conv,
+                                                     complain);
 
       if (val == NULL_TREE)
        val = error_mark_node;
@@ -26567,10 +26579,12 @@ 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);
 
-      {
-       bool removed_attr = false;
-       arg = strip_typedefs_expr (arg, &removed_attr);
-      }
+      arg = convert_nontype_argument_maybe_dependent (tparm, arg,
+                                                     /*forced=*/false,
+                                                     complain);
+      if (!arg || arg == error_mark_node)
+       return unify_invalid (explain_p);
+
       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
       return unify_success (explain_p);
 
diff --git a/gcc/testsuite/g++.dg/template/deduce11.C b/gcc/testsuite/g++.dg/template/deduce11.C
new file mode 100644 (file)
index 0000000..c7671c0
--- /dev/null
@@ -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 (file)
index 0000000..0bd2762
--- /dev/null
@@ -0,0 +1,12 @@
+// 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|does not bind directly|could not convert)" "candidate note" { target c++17 } .-1 }
+// { dg-message "(candidate|not a valid template argument)" "candidate note" { target c++14_down } .-2 }