make detail::bit_cast() constexpr with C++20

This commit is contained in:
Alexey Ochapov
2021-08-27 00:54:52 +03:00
committed by Victor Zverovich
parent fd34a3d246
commit 04b4b69b11

View File

@ -253,8 +253,13 @@ namespace detail {
// undefined behavior (e.g. due to type aliasing).
// Example: uint64_t d = bit_cast<uint64_t>(2.718);
template <typename Dest, typename Source>
inline auto bit_cast(const Source& source) -> Dest {
FMT_CONSTEXPR20 auto bit_cast(const Source& source) -> Dest {
static_assert(sizeof(Dest) == sizeof(Source), "size mismatch");
#ifdef __cpp_lib_bit_cast
if (is_constant_evaluated()) {
return std::bit_cast<Dest>(source);
}
#endif
Dest dest;
std::memcpy(&dest, &source, sizeof(dest));
return dest;