https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112972
Bug ID: 112972
Summary: ambiguity in specification for cast to union types
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: web
Assignee: unassigned at gcc dot gnu.org
Reporter: stephan.stiller at outlook dot com
Target Milestone: ---
On this page of the GCC documentation
https://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html
the statement that
the following assignments
z = (union foo) x;
z = (union foo) y;
are shorthand equivalents of these
z = (union foo) { .i = x };
z = (union foo) { .d = y };
doesn't give the full picture.
While the statement is accurate about
union foo
x
y
z
as defined just above, unions with multiple members of the same type would lead
to a potential ambiguity. For example, for Richard Stallman's example of a
union
union datum {
double latitude;
double longitude;
double height;
double weight;
int continent;
}
(taken from: "GNU C Language Introduction and Reference Manual", Edition 0.0,
section 15.14 (Unions)), casting to this union would be indeterminate with
respect to which of the 4 members of type double is being assigned to.
That is, either (1) first writing to and then reading from identically typed
but differently named members is *not* undefined behavior (and this should be
stated somewhere in the documentation) or (2) a simple "equivalence" between
assignments of the form
p = (union foo) q;
and assignments of the form
p = (union foo) { .m = q };
can't exist (due to the ambiguity of which m applies in the case of there being
multiple members of type typeof(q)).