Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update edm::bit_cast [14.0.x] #45200

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions FWCore/Utilities/interface/bit_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,43 @@
// Created: Wed, 01 Sep 2021 19:11:41 GMT
//

// for compilers that do not support __has_builtin
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif

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

#if __cplusplus >= 202002L

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

#include <bit>

namespace edm {
using std::bit_cast;
} // namespace edm

#elif __has_builtin(__builtin_bit_cast)

// user include files
// before C++20 we can use __builtin_bit_cast, if supported

namespace edm {
//in C++20 we can use std::bit_cast which is constexpr
template <class To, class From>
inline To bit_cast(const From &src) noexcept {
template <typename To, typename 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");
To dst;
std::memcpy(&dst, &src, sizeof(To));
return dst;
return __builtin_bit_cast(To, src);
}
} // namespace edm
#endif

#else

#error constexpr edm::bit_cast is not supported by the compiler

#endif // __cplusplus >= 202002L

#endif // FWCore_Utilities_bit_cast_h