blob: ea956c446055567bc3861c6f4322627295506f38 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#pragma once
#include <cstring>
#include <type_traits>
namespace std {
#ifndef __cpp_lib_bit_cast
#define __cpp_lib_bit_cast 201806L
/// Create a value of type `To` from the bits of `from`.
template<typename To, typename From>
requires (sizeof(To) == sizeof(From)) &&
std::is_trivially_copyable_v<From> &&
std::is_trivially_copyable_v<To>
[[nodiscard]] constexpr To
bit_cast(const From& from) noexcept {
#if __has_builtin(__builtin_bit_cast)
return __builtin_bit_cast(To, from);
#else
static_assert(std::is_trivially_constructible_v<To>);
To to;
std::memcpy(&to, &from, sizeof(To));
return to;
#endif
}
#endif // __cpp_lib_bit_cast
} // namespace std
|