diff options
author | Sven Gothel <[email protected]> | 2023-11-28 12:51:46 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-11-28 12:51:46 +0100 |
commit | 1aaf4f070011490bcece50394b9b32dfa593fd9e (patch) | |
tree | 17d68284e401a35eea3d3a574d986d446a60763a /common/albit.h | |
parent | 6e7cee4fa9a8af03f28ca26cd89f8357390dfc90 (diff) | |
parent | 571b546f35eead77ce109f8d4dd6c3de3199d573 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'common/albit.h')
-rw-r--r-- | common/albit.h | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/common/albit.h b/common/albit.h index ad596208..82a4a00d 100644 --- a/common/albit.h +++ b/common/albit.h @@ -2,7 +2,9 @@ #define AL_BIT_H #include <cstdint> +#include <cstring> #include <limits> +#include <new> #include <type_traits> #if !defined(__GNUC__) && (defined(_WIN32) || defined(_WIN64)) #include <intrin.h> @@ -10,6 +12,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 +{ + alignas(To) char dst[sizeof(To)]; + std::memcpy(&dst[0], &src, sizeof(To)); + return *std::launder(reinterpret_cast<To*>(&dst[0])); +} + #ifdef __BYTE_ORDER__ enum class endian { little = __ORDER_LITTLE_ENDIAN__, |