logoalt Hacker News

el_pollo_diabloyesterday at 9:20 PM1 replyview on HN

Type punning via unions is not UB in C in general, but it is in C++ IIRC.

I write "in general" because, as with other forms of memory reinterpretation (memcpy or copy through a character type), evaluating a trap representation triggers UB.


Replies

Chaosvextoday at 3:11 AM

The short version is that it's fine in C++ as long as you only read the member that was last written to or a char type.