From 64dcb40db139ce1aa79dff16ce863a8d5389199f Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 7 Mar 2023 19:53:32 -0500 Subject: common: make BitCast constexpr --- src/common/bit_cast.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/common/bit_cast.h b/src/common/bit_cast.h index 535148b4d..c6110c542 100644 --- a/src/common/bit_cast.h +++ b/src/common/bit_cast.h @@ -3,19 +3,21 @@ #pragma once -#include -#include +#include + +#ifdef __cpp_lib_bit_cast +#include +#endif namespace Common { template -[[nodiscard]] std::enable_if_t && - std::is_trivially_copyable_v, - To> -BitCast(const From& src) noexcept { - To dst; - std::memcpy(&dst, &src, sizeof(To)); - return dst; +constexpr inline To BitCast(const From& from) { +#ifdef __cpp_lib_bit_cast + return std::bit_cast(from); +#else + return __builtin_bit_cast(To, from); +#endif } } // namespace Common -- cgit v1.2.3