4 ms·
I think that may be inaccurate -- IIRC, in C, you can do type punning via a union but not memcpy, and in C++ you can do type punning via memcpy but not a union
by AaronBallman 6y ago
I think that may be inaccurate -- IIRC, in C, you can do type punning via a union but not memcpy, and in C++ you can do type punning via memcpy but not a union and this incompatibility drives me nuts because it makes inline functions in a header file shared between C and C++ really messy. (Moral of the story: don't pun types.)
- pascal_cuoq 6y agoThe C standard also allows to use memcpy to do type punning: If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one Simply memcpy into a variable (as opposed to dynamically allocated memory). https://port70.net/~nsz/c/c11/n1570.html#6.5p6 https://port70.net/~nsz/c/c11/n1570.html#6.5p6
- AaronBallman 6y agoI must be remembering incorrectly then, thank you!