← Back to context

Comment by ryao

2 days ago

Here is what was said:

> Type punning via unions is undefined behavior in both c and c++.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118141#c13

Feel free to start a discussion on the GCC mailing list.

I actually might, although not now. Thanks for the link. I'm surprised he directly contradicted the C standard, rather than it just being a misunderstanding.

  • According to another comment, the C standard contradicts the C standard on this:

    https://news.ycombinator.com/item?id=43794268

    Taking snippets of the C standard out of context of the whole seems to result in misunderstandings on this.

    • It doesn't. That commenter is saying that in C99, it was unspecified behavior. Since C11 onward, it's been removed from the unspecified behavior annex and type punning is allowed, though it may generate a trap/non-value representation. It was never undefined behavior, which is different.

      Edit: no, it's still in the unspecified behavior annex, that's my mistake. It's still not undefined, though.

      2 replies →