Type Punning in C and C++
Posted by ingve 15 hours ago
Comments
Comment by eqvinox 13 hours ago
(Feels a bit like LLM junk, but honestly not sure.)
Comment by LoganDark 13 hours ago
Feels like LLM to me. I looked at some of the rest of the article and... pretty much certain now.
Comment by rfgplk 13 hours ago
Why leave such disparaging comments?
Comment by munificent 12 hours ago
I try to be as charitable as possible when reading and commenting online, but we don't have infinite attention and thanks to AI, it's easier than ever to end up wasting it on things that aren't worth reading.
(I'm not claiming that the article here is or is not an example of that.)
Comment by quietbritishjim 12 hours ago
Comment by scoopr 13 hours ago
Unfortunately, it seems memcpy is not constexpr, so cannot be used like this, but std::bit_cast actually works[1] as constexpr, so I think in C++ that would now be the most preferred use. It won't allow the union either.
[0] https://www.youtube.com/watch?v=-LAXqqqX274 [1] https://godbolt.org/z/xGzjTMGvv
Comment by leni536 12 hours ago
Also it seems that bit_cast in particular is going to be strengthened in this regard:
Comment by rfgplk 12 hours ago
Comment by antiloper 14 hours ago
What's so genuine about this?
Comment by tialaramex 14 hours ago
Because neither of these are memory safe languages, you are required to ensure you've made no mistakes or else anything might happen.
Comment by ndr 13 hours ago
> and most blog posts on the topic get it wrong.
This smells like Claudism/LLMish.
Comment by StilesCrisis 13 hours ago
I can't downvote posts yet, but if you have the ability, consider it. This is clearly LLM slop without human review.
Comment by eqvinox 12 hours ago
I really hope you're talking mostly about header files; for actual code this is such a horrible idea (and in a lot of cases just won't work without massive efforts.) Even for header files, arguably one ought to really know what they're doing.
Comment by StilesCrisis 12 hours ago
The article never even discusses how C++ actually implements unions to begin with, so it's just incomplete. (Only the "active" member is meaningful, the others are considered as meaningless and shouldn't be touched.)
Comment by LelouBil 13 hours ago
Shouldn't the code return 3 in the base case and 4 if the check for 2 was assumed to always hold ?
Comment by bena 12 hours ago
IIRC, this is all little-endian. So assume our 8 bytes labeled A-H. For a 32 bit/4 byte integer, they will be read as DCBA. For a 64 bit/8 byte integer, HGFEDCBA.
So the struct is set up like a = DBCA, b = HGFE. So when we assign 2 and 3 to a and b, it should look like this in memory:
00000010 00000000 00000000 00000000 and 00000011 00000000 00000000 00000000
When we cast that to a uint64 and assign 4 to it, we should wind up with:
00000100 00000000 00000000 00000000 00000000 00000000 00000000 00000000
Which effectively zeros out b.
So if the conditional is evaluated, it will evaluate to false, we return b, which is 0.
If the conditional is not evaluated, we should return the value in a, which is 4.
Comment by LelouBil 11 hours ago
Comment by pipe01 13 hours ago
Comment by K0IN 14 hours ago
Comment by jasode 14 hours ago
https://stackoverflow.com/questions/53401654/why-was-stdbit-...
Comment by sheafification 14 hours ago
https://en.cppreference.com/cpp/language/reinterpret_cast
EDIT: If you’re able to use C++20 then clearly bit_cast is better.
Comment by leni536 13 hours ago
Comment by Martin_Silenus 12 hours ago
Comment by jefftk 13 hours ago