https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120498
Bug ID: 120498
Summary: error: either all initializer clauses should be
designated or none of them should be when
designated-initialising nested union members(?) (same
code accepted as C)
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: nabijaczleweli at nabijaczleweli dot xyz
Target Milestone: ---
Real code:
static jmp_buf sigbussy;
struct sigaction sa{.sa_flags = static_cast<int>(SA_RESETHAND), .sa_handler =
[](int) { longjmp(sigbussy, true); }};
Extracted:
#include <signal.h>
struct sigaction sa = {.sa_flags = SA_RESETHAND, .sa_handler = 0};
Reduced:
struct sa {
int sa_flags;
union {
int sa_handler, b;
} u;
};
struct sa sa = {.sa_flags = 0, .u.sa_handler = 0};
g++ -std=c++20 fails to compile this with
<source>:7:32: error: either all initializer clauses should be designated or
none of them should be
7 | struct sa sa = {.sa_flags = 0, .u.sa_handler = 0};
| ^
<source>:7:32: error: expected primary-expression before '.' token
Compiler returned: 1
gcc accepts this directly. Repro on 12.2.0, 14.2.0,
Compiler-Explorer-Build-gcc-c7df2b7d4380ade4caf8af4de8d3407d7d523a2f-binutils-2.42)
16.0.0 20250531 (experimental). Probably earlier ones too.
(You can drop the .sa_flags initialiser to get a slightly different error.)
Clang accepts this always. Somehow I feel like GCC-in-C++ mode is wrong to
reject it here.