Type Punning in C and C++
ingve
36 points
23 comments
September 22, 2026
Related Discussions
Found 5 related stories in 75.3ms across 7,406 title embeddings via pgvector HNSW
- Getting silly with C, part and((int*)-8)[3] surprisetalk · 26 pts · August 21, 2026 · 56% similar
- The PImpl idiom and the C++26 std:indirect type signa11 · 66 pts · July 24, 2026 · 52% similar
- Using GCC's Nested Functions with Wide Pointers and No Trampolines II uecker · 92 pts · August 15, 2026 · 49% similar
- An ambiguity in c89 which will never be fixed runningmike · 15 pts · August 10, 2026 · 46% similar
- Show HN: Beautiful Type Erasure with C++26 Reflection RyanJK5 · 112 pts · July 14, 2026 · 43% similar
Discussion Highlights (8 comments)
K0IN
Does reinterpret_cast works in c++ for this?
antiloper
> The C vs C++ distinction here is genuinely treacherous What's so genuine about this?
eqvinox
The example included below "Why C and C++ Differ" is UB in both C and C++ (and says nothing about "why" C & C++ differ). The article isn't overall wrong but that bit is just... (Feels a bit like LLM junk, but honestly not sure.)
pipe01
Is it necessary to use the lowest possible font weight?
scoopr
I just watched video[0] that argues if you can consteval something, then it must be free of UB, which seems compelling, at least for these lower level helpers. 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
StilesCrisis
The conclusion is simply wrong. `union` should not be used for type-punning in C++, or in C code which might be compiled by a C++ compiler. I can't downvote posts yet, but if you have the ability, consider it. This is clearly LLM slop without human review.
LelouBil
I don't understand the example in "Why C and C++ Differ". Shouldn't the code return 3 in the base case and 4 if the check for 2 was assumed to always hold ?
Martin_Silenus
Hackers don't make Quake III Arena by trusting C rules or compiler optimizations anyway.