- 171comments
- 83comments
- 437comments
- 92comments
- 129comments
- 135comments
- 309comments
- 270comments
- 75comments
- 357comments
- 22comments
- 42comments
- 12comments
- 332comments
- 18comments
- 3comments
- 321comments
- 20comments
- 32comments
- 340comments
- 84comments
- 15comments
- 2comments
- 2comments
- 75comments
- 98comments
- 64comments
- 37comments
- 39comments
- 68comments
Does reinterpret_cast works in c++ for this?
Yeah, you could use it for that, but it’s got some caveats and is unsafe in some cases.
https://en.cppreference.com/cpp/language/reinterpret_cast
EDIT: If you’re able to use C++20 then clearly bit_cast is better.
reinterpret_cast<> for punning purposes can lead to UB. The recommended C++ alternative to avoid UB is bit_cast<>
https://stackoverflow.com/questions/53401654/why-was-stdbit-...
No.
What's so genuine about this?
Despite the fact they're two different languages, people are often taught as though it's basically just a subset relationship and then they carry this through into the actual software they write.
Because neither of these are memory safe languages, you are required to ensure you've made no mistakes or else anything might happen.
It continues with
This smells like Claudism/LLMish.
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.)
Feels like LLM to me. I looked at some of the rest of the article and... pretty much certain now.
Is it necessary to use the lowest possible font weight?
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
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.
[delayed]