3 ms·
The best way to do this in C++ is using std::bit_cast, which unfortunately only became available in C++20. Before that, you have to memcpy. That works just fine
by OskarS 5y ago
The best way to do this in C++ is using std::bit_cast, which unfortunately only became available in C++20. Before that, you have to memcpy. That works just fine, but the syntax is a bit clunky and it doesn’t work in constexpr contexts, which is why std::bit_cast was introduced.
Compilers will optimize all of this to the same machine code.