Hi,

this rejects-valid issue, as submitted, has only the testcase:

union Test {
  static constexpr int kConstant = 10;
};

which seems easy to handle by adjusting check_field_decls. While doing that, I also noticed that the same piece of C++98 which went away in C++11, thus allowing static members in unions, also mentioned reference-types, which therefore, should be now also allowed (current ICC agrees, current clang disagrees). Tested x86_64-linux.

Thanks,
Paolo.

////////////////////
/cp
2014-06-06  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/60184
        * class.c (check_field_decls): In C++11 mode do not reject constexpr
        static members and reference-type members of unions.

/testsuite
2014-06-06  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/60184
        * g++.dg/cpp0x/constexpr-union6.C: New.
        * g++.dg/cpp0x/union6.C: Likewise.
        * g++.dg/init/ref14.C: Adjust.
        * g++.dg/init/union1.C: Likewise.
Index: cp/class.c
===================================================================
--- cp/class.c  (revision 211309)
+++ cp/class.c  (working copy)
@@ -3480,21 +3480,25 @@ check_field_decls (tree t, tree *access_decls,
       /* When this goes into scope, it will be a non-local reference.  */
       DECL_NONLOCAL (x) = 1;
 
-      if (TREE_CODE (t) == UNION_TYPE)
+      if (TREE_CODE (t) == UNION_TYPE
+         && cxx_dialect < cxx11)
        {
-         /* [class.union]
+         /* [class.union] (C++98)
 
             If a union contains a static data member, or a member of
-            reference type, the program is ill-formed.  */
+            reference type, the program is ill-formed.
+
+            In C++11 this limitation doesn't exist anymore.  */
          if (VAR_P (x))
            {
-             error ("%q+D may not be static because it is a member of a 
union", x);
+             error ("in C++98 %q+D may not be static because it is "
+                    "a member of a union", x);
              continue;
            }
          if (TREE_CODE (type) == REFERENCE_TYPE)
            {
-             error ("%q+D may not have reference type %qT because"
-                    " it is a member of a union",
+             error ("in C++98 %q+D may not have reference type %qT "
+                    "because it is a member of a union",
                     x, type);
              continue;
            }
Index: testsuite/g++.dg/cpp0x/constexpr-union6.C
===================================================================
--- testsuite/g++.dg/cpp0x/constexpr-union6.C   (revision 0)
+++ testsuite/g++.dg/cpp0x/constexpr-union6.C   (working copy)
@@ -0,0 +1,10 @@
+// PR c++/60184
+// { dg-do compile { target c++11 } }
+
+union Test1 {
+  static constexpr int kConstant = 10;
+};
+
+union Test2 {
+  static constexpr const int& kConstant = Test1::kConstant;
+};
Index: testsuite/g++.dg/cpp0x/union6.C
===================================================================
--- testsuite/g++.dg/cpp0x/union6.C     (revision 0)
+++ testsuite/g++.dg/cpp0x/union6.C     (working copy)
@@ -0,0 +1,20 @@
+// PR c++/60184
+// { dg-do compile { target c++11 } }
+
+union Test1 {
+  static int kConstant;
+};
+
+union Test2 {
+  static const int kConstant;
+};
+
+const int Test2::kConstant = 10;
+
+union Test3 {
+  int& kConstant;
+};
+
+union Test4 {
+  const int& kConstant = 10;
+};
Index: testsuite/g++.dg/init/ref14.C
===================================================================
--- testsuite/g++.dg/init/ref14.C       (revision 211309)
+++ testsuite/g++.dg/init/ref14.C       (working copy)
@@ -4,7 +4,7 @@
 
 union A
 {
-  int &i; // { dg-error "may not have reference type" }
+  int &i; // { dg-error "may not have reference type" "" { target { ! c++11 } 
} }
 };
 
 void foo()
Index: testsuite/g++.dg/init/union1.C
===================================================================
--- testsuite/g++.dg/init/union1.C      (revision 211309)
+++ testsuite/g++.dg/init/union1.C      (working copy)
@@ -1,5 +1,5 @@
 // PR c++/14401
 
 union U {
-  int& i; // { dg-error "" }
+  int& i; // { dg-error "reference type" "" { target { ! c++11 } } }
 };

Reply via email to