Skip to content

Commit

Permalink
Update edm::bit_cast
Browse files Browse the repository at this point in the history
Use std::bit_cast in C++20 mode. Othwerwise, use __builtin_bit_cast if available.
  • Loading branch information
fwyzard committed Jun 7, 2024
1 parent 4f141ff commit 0b3a0f4
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions FWCore/Utilities/interface/bit_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,39 @@

// system include files
#include <cstring>
#include <type_traits>

// user include files
#if __cplusplus >= 202002L

// in C++20 we can use std::bit_cast

#include <bit>

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 <class To, class From>
constexpr
inline To bit_cast(const From &src) noexcept {
static_assert(std::is_trivially_copyable_v<From>);
static_assert(std::is_trivially_copyable_v<To>);
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

0 comments on commit 0b3a0f4

Please sign in to comment.