https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117614
Bug ID: 117614
Summary: Cannot change active member of anonymous union in
constant expression
Product: gcc
Version: 14.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: fchelnokov at gmail dot com
Target Milestone: ---
This program
```
constexpr int f() {
union {
int x{0};
char y;
};
y = 1;
return y;
}
static_assert( f() == 1 );
```
is accepted in Clang and GCC 13.3, but GCC 14.2 complains:
<source>:6:7: error: accessing 'f()::<unnamed union>::y' member instead of
initialized 'f()::<unnamed union>::x' member in constant expression
6 | y = 1;
| ~~^~~
<source>:4:14: note: initializing 'f()::<unnamed union>::y' requires a member
access expression as the left operand of the assignment
Online demo: https://gcc.godbolt.org/z/6h9W5fjh1