C++ Logo

std-discussion

Advanced search

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

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Fri, 17 Mar 2023 01:43:27 +0500
> 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?

`s` could be just `int s`, and it wouldn't change much, this question is just about «accessing object representation».

Received on 2023-03-16 20:43:32