https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87363

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
If u's initialization is viewed as a single assignment then you're right.  If
the initialization of u is viewed as two separate assignments:

  u.x = "1234";
  u.y = "567";

then strictly speaking, after the second of the two, the first four bytes of
u.z have an unspecified value (even though u.x must remain unchanged).  In
either case, if the size of the two union members isn't the same the result of
accessing the the larger one is unquestionably unspecified.  In C++, the access
is undefined regardless of their size.

As with many test cases contrived to find compiler bugs by exploiting the dark
corners of the language, the answer depends on one's interpretation, and the
solution (warn or not, and what to say in the warning) on how likely fragile
code like this is a useful idiom rather than a coding mistake hiding a bug.

Reply via email to