[cfe-commits] r151394 - in /cfe/trunk: lib/Sema/SemaDeclCXX.cpp test/SemaCXX/cxx0x-deleted-default-ctor.cpp

Douglas Gregor dgregor at apple.com
Fri Feb 24 13:25:54 PST 2012


Author: dgregor
Date: Fri Feb 24 15:25:53 2012
New Revision: 151394

URL: http://llvm.org/viewvc/llvm-project?rev=151394&view=rev
Log:
C++11 [class.ctor]p5 says that
  A defaulted default constructor for a class X is defined as deleted if [...]
    -  X is a union and all of its variant members are of const-qualified type.

A pedantic reading therefore says that

 union X { };

has a deleted default constructor, which is both silly and almost
certainly unintended. Pretend as if this this read

    - X is a union with one or more variant members, and all of its
      variant members are of const-qualified type. 


Modified:
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/cxx0x-deleted-default-ctor.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=151394&r1=151393&r2=151394&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Fri Feb 24 15:25:53 2012
@@ -4531,7 +4531,8 @@
       }
 
       // At least one member in each anonymous union must be non-const
-      if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst)
+      if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst &&
+          FieldRecord->field_begin() != FieldRecord->field_end())
         return true;
 
       // Don't try to initialize the anonymous union
@@ -4611,7 +4612,10 @@
 ///   A defaulted default constructor for a class X is defined as deleted if
 /// X is a union and all of its variant members are of const-qualified type.
 bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() {
-  return CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst;
+  // This is a silly definition, because it gives an empty union a deleted
+  // default constructor. Don't do that.
+  return CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst &&
+    (MD->getParent()->field_begin() != MD->getParent()->field_end());
 }
 
 /// Determine whether a defaulted special member function should be defined as

Modified: cfe/trunk/test/SemaCXX/cxx0x-deleted-default-ctor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx0x-deleted-default-ctor.cpp?rev=151394&r1=151393&r2=151394&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/cxx0x-deleted-default-ctor.cpp (original)
+++ cfe/trunk/test/SemaCXX/cxx0x-deleted-default-ctor.cpp Fri Feb 24 15:25:53 2012
@@ -121,11 +121,11 @@
 
 // See also rdar://problem/8125400.
 namespace empty {
-  static union {}; // expected-error {{implicitly-deleted default constructor}} expected-note {{here}}
-  static union { union {}; }; // expected-error {{implicitly-deleted default constructor}} expected-note {{here}}
+  static union {};
+  static union { union {}; };
   static union { struct {}; };
-  static union { union { union {}; }; }; // expected-error {{implicitly-deleted default constructor}} expected-note {{here}}
+  static union { union { union {}; }; };
   static union { union { struct {}; }; };
-  static union { struct { union {}; }; }; // expected-error {{implicitly-deleted default constructor}} expected-note {{here}}
+  static union { struct { union {}; }; };
   static union { struct { struct {}; }; };
 }





More information about the cfe-commits mailing list