Re:The Byte Order Fiasco
by jeffreygoesto on Saturday May 08, 2021 @12:58PM
(#27088837)
Wouldn't that cast be UB because it is type punning?
[ Parent
| on HN ]
No, because no punning exists here. The code is C++, so this calls a conversion function that likely does the bit manipulation internally in a legal way.
Re:The Byte Order Fiasco
by professoretc on Saturday May 08, 2021 @01:00PM
(#27088861)
char* is a allowed to alias to other pointer types.
Re:The Byte Order Fiasco
by jeffreygoesto on Saturday May 08, 2021 @03:05PM
(#27089844)
Hm. Afsik, you are always allowed to convert _to_ a char, but _from_ is not ok in general. See i.e. [0]
[0] https://gist.github.com/shafik/848ae25ee209f698763cffee272a5...
Why is it not ok to convert from a char? Some of the information in the gist is wrong. Type punning with unions for example is legal. ANSI X3.159-1988 is quite clear on that point in its aliasing rules. I've seen a lot of comments people post online saying you must use memcpy to read the bits in a float or that c++ forbids union punning but where is that written. Since if that were true every math library would break.