From 0b3a0f44feeff034faa8eb6e14a0128d0727d54c Mon Sep 17 00:00:00 2001 From: Andrea Bocci Date: Fri, 7 Jun 2024 11:15:54 +0200 Subject: [PATCH] Update edm::bit_cast Use std::bit_cast in C++20 mode. Othwerwise, use __builtin_bit_cast if available. --- FWCore/Utilities/interface/bit_cast.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/FWCore/Utilities/interface/bit_cast.h b/FWCore/Utilities/interface/bit_cast.h index d8301a7100b06..08b218a5b03fa 100644 --- a/FWCore/Utilities/interface/bit_cast.h +++ b/FWCore/Utilities/interface/bit_cast.h @@ -20,17 +20,39 @@ // system include files #include +#include -// user include files +#if __cplusplus >= 202002L + +// in C++20 we can use std::bit_cast + +#include + +namespace edm { + using std::bit_cast; +} // namespace edm + +#else + +// before C++20 we can use __builtin_bit_cast in recent compilers namespace edm { - //in C++20 we can use std::bit_cast which is constexpr template + constexpr inline To bit_cast(const From &src) noexcept { + static_assert(std::is_trivially_copyable_v); + static_assert(std::is_trivially_copyable_v); static_assert(sizeof(To) == sizeof(From), "incompatible types"); +#ifdef GCC_PREREQUISITE(11,0,0) || CLANG_PREREQUISITE(9,0,0) + return __builtin_bit_cast(To, src); +#else To dst; std::memcpy(&dst, &src, sizeof(To)); return dst; +#endif } } // namespace edm + +#endif // __cplusplus >= 202002L + #endif