diff mbox

C++ PATCH for c++/51747 (list-initialization from same type)

Message ID 53483236.7030700@redhat.com
State New
Headers show

Commit Message

Jason Merrill April 11, 2014, 6:19 p.m. UTC
Recent changes to the C++ standard have allowed the use of 
list-initialization with a single initializer of the same type as the 
target; this patch updates reshape_init accordingly.

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

Comments

Marc Glisse April 14, 2014, 10:02 p.m. UTC | #1
On Fri, 11 Apr 2014, Jason Merrill wrote:

> Recent changes to the C++ standard have allowed the use of 
> list-initialization with a single initializer of the same type as the target; 
> this patch updates reshape_init accordingly.
>
> Tested x86_64-pc-linux-gnu, applying to trunk.

Hello,

shouldn't the same also apply if VECTOR_TYPE_P (type), not just for 
CLASS_TYPE_P (type)?

Testcase in:
http://stackoverflow.com/questions/23070982/c-initialization-of-intel-simd-intrinsics-class-members
Jason Merrill April 15, 2014, 2:01 p.m. UTC | #2
On 04/14/2014 06:02 PM, Marc Glisse wrote:
> shouldn't the same also apply if VECTOR_TYPE_P (type), not just for
> CLASS_TYPE_P (type)?

Sure.  Do you want to make that change?

Jason
Marc Glisse April 15, 2014, 2:13 p.m. UTC | #3
On Tue, 15 Apr 2014, Jason Merrill wrote:

> On 04/14/2014 06:02 PM, Marc Glisse wrote:
>> shouldn't the same also apply if VECTOR_TYPE_P (type), not just for
>> CLASS_TYPE_P (type)?
>
> Sure.  Do you want to make that change?

I can add || VECTOR_TYPE_P (type), yes, but I thought you might have ideas 
about other cases that might have been forgotten, maybe arrays or 
something (I didn't have time to test any further), and thus on what the 
right test should be. If it is just vectors I'll prepare a patch with a 
simple testcase.
Jason Merrill April 15, 2014, 2:59 p.m. UTC | #4
On 04/15/2014 10:13 AM, Marc Glisse wrote:
> I can add || VECTOR_TYPE_P (type), yes, but I thought you might have
> ideas about other cases that might have been forgotten, maybe arrays or
> something (I didn't have time to test any further), and thus on what the
> right test should be. If it is just vectors I'll prepare a patch with a
> simple testcase.

It's just vectors, because they're an extension; the patch I checked in 
covered the standard language.

Jason
diff mbox

Patch

commit 0bb6493b9f08021d00a636fe5b4ea777bd4cbc13
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Mar 21 06:15:02 2014 -0400

    	DR 1467
    	PR c++/51747
    	* decl.c (reshape_init_r): Handle a single element of class type.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 069b374..f8ae07c 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -5405,6 +5405,18 @@  reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
       return init;
     }
 
+  /* "If T is a class type and the initializer list has a single element of
+     type cv U, where U is T or a class derived from T, the object is
+     initialized from that element."  Even if T is an aggregate.  */
+  if (cxx_dialect >= cxx11 && CLASS_TYPE_P (type)
+      && first_initializer_p
+      && d->end - d->cur == 1
+      && reference_related_p (type, TREE_TYPE (init)))
+    {
+      d->cur++;
+      return init;
+    }
+
   /* [dcl.init.aggr]
 
      All implicit type conversions (clause _conv_) are considered when
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist83.C b/gcc/testsuite/g++.dg/cpp0x/initlist83.C
new file mode 100644
index 0000000..4a5eeb6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist83.C
@@ -0,0 +1,7 @@ 
+// DR 1467, c++/51747
+// { dg-do compile { target c++11 } }
+
+struct X { };
+
+X x;
+X x2{x};
diff --git a/gcc/testsuite/g++.dg/init/aggr4.C b/gcc/testsuite/g++.dg/init/aggr4.C
index 7120e68..b0eae2e 100644
--- a/gcc/testsuite/g++.dg/init/aggr4.C
+++ b/gcc/testsuite/g++.dg/init/aggr4.C
@@ -4,4 +4,4 @@  struct A
 };
 
 A a1 = { 1 };			// ok
-A a2 = { a1 };			// { dg-error "cannot convert" }
+A a2 = { a1 };	 // { dg-error "cannot convert" "" { target { ! c++11 } } }