C++ Logo

std-discussion

Advanced search

Re: UB in P2641 'Checking if a union alternative is active'

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Mon, 19 Jun 2023 21:27:55 +0200
On 19/06/2023 19.59, Barry Revzin via Std-Discussion wrote:
> Given:
>
> int main() {
> union U { int i; char c; };
> U u{.i=1};
>
> char c1 = (char&)u;
> char c2 = (char&)u.i;
> char c3 = u.c;
> return c1 + c2 + c3;
> }
>
> c1, c2, and c3 are obviously equivalent here. If there's wording to suggest otherwise, that's a wording defect.

Note that this uses the "object representation" and aliasing special-cases for "char";
unfortunately, there are some specification holes in that area.

Jens

Received on 2023-06-19 19:28:00