]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: CWG 2229, cv-qualified unnamed bit-fields [PR123935]
authorMarek Polacek <polacek@redhat.com>
Tue, 3 Feb 2026 03:07:47 +0000 (22:07 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 23 Apr 2026 15:13:57 +0000 (11:13 -0400)
This implements [class.bit]/2: An unnamed bit-field shall not be
declared with a cv-qualified type.  This was clarified in DR 2229.

DR 2229
PR c++/123935

gcc/cp/ChangeLog:

* decl2.cc (grokbitfield): Add pedwarn for cv-qualified unnamed
bit-fields.

gcc/testsuite/ChangeLog:

* g++.dg/DRs/dr2229.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/decl2.cc
gcc/testsuite/g++.dg/DRs/dr2229.C [new file with mode: 0644]

index 351e1c3766fa05611d85b4057bc2dd41a9b2d03f..a0bc57d4a06d8c8581e41a8052bb5098afdfbb05 100644 (file)
@@ -1634,6 +1634,12 @@ grokbitfield (const cp_declarator *declarator,
       return NULL_TREE;
     }
 
+  /* [class.bit]/2 "An unnamed bit-field shall not be declared with
+     a cv-qualified type."  */
+  if (!DECL_NAME (value) && TYPE_QUALS (type) != TYPE_UNQUALIFIED)
+    pedwarn (DECL_SOURCE_LOCATION (value), 0,
+            "unnamed bit-field cannot be cv-qualified");
+
   int flags = LOOKUP_IMPLICIT;
   if (init && DIRECT_LIST_INIT_P (init))
     flags = LOOKUP_NORMAL;
diff --git a/gcc/testsuite/g++.dg/DRs/dr2229.C b/gcc/testsuite/g++.dg/DRs/dr2229.C
new file mode 100644 (file)
index 0000000..a55604b
--- /dev/null
@@ -0,0 +1,23 @@
+// DR 2229, Volatile unnamed bit-fields
+// PR c++/123935
+
+typedef const unsigned cu;
+typedef volatile unsigned vu;
+typedef const volatile unsigned cvu;
+
+struct S {
+  const unsigned : 1; // { dg-error "unnamed bit-field cannot be cv-qualified" }
+  volatile unsigned : 1; // { dg-error "unnamed bit-field cannot be cv-qualified" }
+  const volatile unsigned : 1; // { dg-error "unnamed bit-field cannot be cv-qualified" }
+  unsigned : 1;
+  const unsigned i1 : 1;
+  volatile unsigned i2 : 1;
+  const volatile unsigned i3 : 1;
+
+  cu : 1;   // { dg-error "unnamed bit-field cannot be cv-qualified" }
+  vu : 1;   // { dg-error "unnamed bit-field cannot be cv-qualified" }
+  cvu : 1;  // { dg-error "unnamed bit-field cannot be cv-qualified" }
+  cu i4: 1;
+  vu i5 : 1;
+  cvu i6 : 1;
+};