diff options
Diffstat (limited to 'common/albit.h')
-rw-r--r-- | common/albit.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/common/albit.h b/common/albit.h index ad596208..a563a4e7 100644 --- a/common/albit.h +++ b/common/albit.h @@ -2,6 +2,7 @@ #define AL_BIT_H #include <cstdint> +#include <cstring> #include <limits> #include <type_traits> #if !defined(__GNUC__) && (defined(_WIN32) || defined(_WIN64)) @@ -10,6 +11,16 @@ namespace al { +template<typename To, typename From> +std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> + && std::is_trivially_copyable_v<To>, +To> bit_cast(const From &src) noexcept +{ + union { char c; To dst; } u; + std::memcpy(&u.dst, &src, sizeof(To)); + return u.dst; +} + #ifdef __BYTE_ORDER__ enum class endian { little = __ORDER_LITTLE_ENDIAN__, |