3 ms·
I'm not sure what you're getting at but const bool z = (const bool)((int8_t)2); Is perfectly valid C++.
by poppadom1982 6mo ago
I'm not sure what you're getting at but
const bool z = (const bool)((int8_t)2);
Is perfectly valid C++.
- debugnik 6mo agoThat's a conversion, not the same. The naive equivalent to transmute would be int8_t x = 2; bool y = *reinterpret_cast<bool *>(&x); But reinterpret_cast isn't valid in a constexpr scope.
- poppadom1982 6mo agoMy point is, in your exact example both reinterpret_cast and C-style casts have the exact same behavior, making the example bad. If you want to showcase a deficiency of C++, it would make sense to pick something where the difference between cast types actually matters.
- TuxSH 6mo ago> But reinterpret_cast isn't valid in a constexpr scope. std::bit_cast is
- debugnik 6mo agoOh cool, and it behaves like memcpy, not like pointer aliasing! I'm stuck with C++14 at work so I missed that one.