mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-29 18:27:40 +02:00
make detail::bit_cast() constexpr with C++20
This commit is contained in:
committed by
Victor Zverovich
parent
fd34a3d246
commit
04b4b69b11
@ -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;
|
||||
|
Reference in New Issue
Block a user