diff mbox

C++ PATCH for c++/63283 (constexpr call with reference parameter in template)

Message ID 54B826B8.2080908@redhat.com
State New
Headers show

Commit Message

Jason Merrill Jan. 15, 2015, 8:44 p.m. UTC
Normally when we see a call to a function with a reference parameter, 
we've converted the argument to have reference type as well, so we can 
always treat arguments as rvalues in potential_constant_expression.  But 
with templates we defer the conversions until instantiation time, so we 
shouldn't require the argument to be an rvalue constant yet.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit dbb548c7d0adf94504c732f92120c78186834e1b
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jan 14 19:34:04 2015 -0500

    	PR c++/63283
    	* constexpr.c (potential_constant_expression_1): Handle reference
    	args in templates.

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 1432506..e27a892 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -3881,7 +3881,11 @@  potential_constant_expression_1 (tree t, bool want_rval, bool strict,
         for (; i < nargs; ++i)
           {
             tree x = get_nth_callarg (t, i);
-	    if (!RECUR (x, rval))
+	    /* In a template, reference arguments haven't been converted to
+	       REFERENCE_TYPE and we might not even know if the parameter
+	       is a reference, so accept lvalue constants too.  */
+	    bool rv = processing_template_decl ? any : rval;
+	    if (!RECUR (x, rv))
 	      return false;
           }
         return true;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-template8.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-template8.C
new file mode 100644
index 0000000..7b2b9c7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-template8.C
@@ -0,0 +1,10 @@ 
+// PR c++/63283
+// { dg-do compile { target c++11 } }
+
+constexpr int array_length(int (&array)[3]) { return 3; }
+int a[] = { 1, 2, 3 };
+template <typename T> int f() {
+  struct { int e[array_length(a)]; } t;
+  return sizeof(t);
+}
+int main() { f<void>(); }
diff --git a/gcc/testsuite/g++.dg/cpp0x/static_assert10.C b/gcc/testsuite/g++.dg/cpp0x/static_assert10.C
index 216f259..e7f728e 100644
--- a/gcc/testsuite/g++.dg/cpp0x/static_assert10.C
+++ b/gcc/testsuite/g++.dg/cpp0x/static_assert10.C
@@ -4,5 +4,5 @@ 
 template<typename T> bool foo(T)
 {
   int i;
-  static_assert(foo(i), "Error"); // { dg-error "non-constant condition|not usable" }
+  static_assert(foo(i), "Error"); // { dg-error "non-constant condition|not usable|non-constexpr" }
 }