[Bug c/90167] invalid example in GCC documentation wrt. effective type rules
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90167 --- Comment #4 from Laszlo Ersek (RH) --- So one way to define the behavior for the original example (from the gcc docs) would be: int f(void) { double d = 3.0; union a_union u = *(union a_union *) return u.i; } Thanks.
[Bug c/90167] invalid example in GCC documentation wrt. effective type rules
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90167 --- Comment #3 from Segher Boessenkool --- But you are not accessing as the union type. You do the access with the type of one of its members. And that is UB. The part of the standard you quote is about things like union a_union f(double *p) { return *(union a_union *)p; }
[Bug c/90167] invalid example in GCC documentation wrt. effective type rules
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90167 --- Comment #2 from Laszlo Ersek (RH) --- (In reply to Segher Boessenkool from comment #1) > The code accesses d, of type double, as an int. That is not a > compatible type. Agreed; I didn't claim it was. > It does not matter how it got there, what pointer casts trickery with > unions it did. I disagree, and in my opinion, the standard disagrees too. > You can access a union type as the type of any of its members. But a > double is not a union type. I didn't claim it was. The standard writes, An object [the double] shall have its stored value accessed only by an lvalue expression that has one of the following types: [...] - a [...] union type that includes [a type compatible with the effective type of the [double] object] among its members It says we can access a "double" through a union which has a "double" member. union u { int i; double d1; }; double d2; The expression (*(union u *)) is an lvalue expression that has a union type that includes a double among its members. To me this seems to follow from the letter of the standard. If my interpretation is incorrect, or the standard is unclear or incorrect, please show that. Thanks.
[Bug c/90167] invalid example in GCC documentation wrt. effective type rules
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90167 Segher Boessenkool changed: What|Removed |Added Status|UNCONFIRMED |RESOLVED CC||segher at gcc dot gnu.org Resolution|--- |INVALID --- Comment #1 from Segher Boessenkool --- The code accesses d, of type double, as an int. That is not a compatible type. It does not matter how it got there, what pointer casts trickery with unions it did. You can access a union type as the type of any of its members. But a double is not a union type.