C++ Logo

std-discussion

Advanced search

Value of non-active data member byte in constexpr union?

From: Stephan Bergmann <sbergman_at_[hidden]>
Date: Thu, 16 Mar 2023 21:20:33 +0100
I'm trying to understand what the semantics of the C++20 program

> struct S {
> union {
> char a[2];
> char b[1];
> };
> constexpr S() {
> a[1] = 2;
> b[0] = 3;
> }
> };
> constexpr S s;
> int main() {
> return reinterpret_cast<unsigned char const *>(&s)[1];
> }

is, but I fail to find anything relevant in the standard. Plausible
results could be UB, "return 0" (which is what at least Clang and GCC
appear to agree on), or even "return 2". Any pointers?

Received on 2023-03-16 20:20:40