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
--
Std-Discussion mailing list
Std-Discussion@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
To elaborate on what Jens said,
It is a known issue that the current wording of the standard does not technically permit a user to implement their own `memcpy`, because there is no provision in the language to be able to read a byte of an object representation through a char glvalue; see
P1839 for discussion. The intent is to fix this wording gap eventually, but it's not so easy.
It is assumed that we will find some kind of solution to this problem, in which the naive strategy of `reinterpret_cast`ing to `char*` and then reading through that pointer will actually be well-defined and yield the object representation, and whatever the resolution is, it will be considered as a DR.
If that's the case, then you can use a `char` glvalue to read the object representation of a `U` object, notwithstanding the fact that the active member is something other than a `char`. In the meantime, you can rely on this not being UB for all practical purposes, even though the wording to make it well-defined has not been figured out yet.
--