Closed
Description
Code
consteval void foo(){
union { struct {int i1; int i2; int i3; int i4;} s;
struct {int i1; int i2; int i3; int i4;} s2;
int i[4];
} u{};
auto sum = u.s2.i1+u.s2.i1+u.s2.i1+u.s2.i1;
}
int main(){
foo();
}
produces error with
note: read of member 's2' of union with active member 's' is not allowed in a constant expression
7 | auto sum = u.s2.i1+u.s2.i1+u.s2.i1+u.s2.i1;
| ^
https://godbolt.org/z/dcPWW8ncx
(this tested from clang-x86_64 10.0.0 to clang-x86_64 20.1.0 and trunk version on godbolt)
but:
https://eel.is/c++draft/class.union#general-note-1
https://eel.is/c++draft/class.mem#general-27
https://eel.is/c++draft/class.prop#10
https://eel.is/c++draft/class.mem#def:layout-compatible,class
https://eel.is/c++draft/class.mem#general-29
the s
and s2
union members are two standard-layout struct types and have common initial sequence (and what's more: they are layout-compatible).
in my opinion the error behavior here is a bug.