Patchwork C++ PATCH to allow VLAs with C++0x auto

login
register
mail settings
Submitter Jason Merrill
Date Aug. 5, 2011, 7:10 p.m.
Message ID <4E3C400C.9020305@redhat.com>
Download mbox | patch
Permalink /patch/108714/
State New
Headers show

Comments

Jason Merrill - Aug. 5, 2011, 7:10 p.m.
Paolo asked for GCC to allow deduction of auto from a variable-length 
array.  Since auto doesn't have the issues involved with normal template 
deduction from VLAs (namely, the type not being link-time constant), 
this seems reasonable to me.

Tested x86_64-pc-linux-gnu, applying to trunk.
Paolo Carlini - Aug. 6, 2011, 11:09 a.m.
On 08/05/2011 09:10 PM, Jason Merrill wrote:
> Paolo asked for GCC to allow deduction of auto from a variable-length 
> array.  Since auto doesn't have the issues involved with normal 
> template deduction from VLAs (namely, the type not being link-time 
> constant), this seems reasonable to me.
Thanks!

Paolo.

Patch

commit 002b9c9ad8b14999fa87c65f3ccdce772086edd8
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Aug 4 11:48:40 2011 -0400

    	* pt.c (unify) [TEMPLATE_TYPE_PARM]: Allow VLA for C++0x 'auto'.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 571da6d..10fdced 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -15932,10 +15932,11 @@  unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 	     that were talking about variable-sized arrays (like
 	     `int[n]'), rather than arrays of unknown size (like
 	     `int[]').)  We'll get very confused by such a type since
-	     the bound of the array will not be computable in an
-	     instantiation.  Besides, such types are not allowed in
-	     ISO C++, so we can do as we please here.  */
-	  if (variably_modified_type_p (arg, NULL_TREE))
+	     the bound of the array is not constant, and therefore
+	     not mangleable.  Besides, such types are not allowed in
+	     ISO C++, so we can do as we please here.  We do allow
+	     them for 'auto' deduction, since that isn't ABI-exposed.  */
+	  if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
 	    return unify_vla_arg (explain_p, arg);
 
 	  /* Strip typedefs as in convert_template_argument.  */
diff --git a/gcc/testsuite/g++.dg/ext/vla11.C b/gcc/testsuite/g++.dg/ext/vla11.C
new file mode 100644
index 0000000..8f3be9e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/vla11.C
@@ -0,0 +1,8 @@ 
+// Test that auto works with VLAs.
+// { dg-options -std=c++0x }
+
+void bar(int n)
+{
+  float loc2[n];
+  auto&& range = loc2;
+}