3 ms·
There's another, not in the C++ language but in the C++ standard library, called bit_cast: https://en.cppreference.com/w/cpp/numeric/bit_cast https://en.cpprefe
by kccqzy 7y ago
There's another, not in the C++ language but in the C++ standard library, called bit_cast: https://en.cppreference.com/w/cpp/numeric/bit_cast https://en.cppreference.com/w/cpp/numeric/bit_cast
It essentially just calls memcpy after some basic checking. It's similar to reinterpret_cast in spirit but more powerful. You can convert for example a float array into a char array (I don't think any of the four standard casts will allow you to convert a whole array at a time). Compilers like clang will generally recognize memcpy as __builtin_memcpy which sometimes generates no instructions at all.
- dirtydroog 7y agoI suggested an underlying_cast function that converts a typed-enum to its underlying type. It's quite useful for things like sending typed enums to streams.
- saagarjha 7y agoThat's just static_cast<std::underlying_type_t<EnumType>>(value), right?
- dirtydroog 7y agoYes, pretty much. But everyone will probably end up writing this at some point. It may as well be in the standard library.