std::bit_cast is often presented as the correct alternative to type punning in C++. However, we see people use it as a drop-in replacement to reinterpret_cast , which only obfuscates the code and still has UB:
float x{};
-int* y = reinterpret_cast<int*>(&x);
+int* y = std::bit_cast<int*>(&x);
This usage is incorrect. Instead, the code should do:
int y = std::bit_cast<int>(x);
Most likely all usages of std::bit_cast<PointerType> are incorrect.
`std::bit_cast` is often presented as the correct alternative to type punning in C++. However, we see people use it as a drop-in replacement to `reinterpret_cast` , which only obfuscates the code and still has UB:
```cpp
float x{};
-int* y = reinterpret_cast<int*>(&x);
+int* y = std::bit_cast<int*>(&x);
```
This usage is incorrect. Instead, the code should do:
```cpp
int y = std::bit_cast<int>(x);
```
Most likely all usages of `std::bit_cast<PointerType>` are incorrect.
std::bit_cast
is often presented as the correct alternative to type punning in C++. However, we see people use it as a drop-in replacement toreinterpret_cast
, which only obfuscates the code and still has UB:This usage is incorrect. Instead, the code should do:
Most likely all usages of
std::bit_cast<PointerType>
are incorrect.