-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_cast.h
More file actions
68 lines (59 loc) · 2.52 KB
/
Copy pathbit_cast.h
File metadata and controls
68 lines (59 loc) · 2.52 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef BIT_CAST_H
#define BIT_CAST_H
#include <cstring>
#include <type_traits>
// TODO: Use is_trivially_copyable_v and other type traits when my compiler
// implements more of C++17.
#if defined(__cpp_concepts) && __cpp_concepts >= 201507
# define BIT_CAST_CONCEPTS(TO, FROM) requires \
sizeof(TO) == sizeof(FROM) && \
std::is_trivially_copyable<TO>::value && \
std::is_trivially_copyable<FROM>::value
#else
# define BIT_CAST_CONCEPTS(TO, FROM)
#endif
#if defined(BIT_CAST_USE_SFINAE)
# define BIT_CAST_ENABLE_IF(TO, FROM) , \
typename = std::enable_if_t<sizeof(TO) == sizeof(FROM)>, \
typename = std::enable_if_t<std::is_trivially_copyable<TO>::value>, \
typename = std::enable_if_t<std::is_trivially_copyable<FROM>::value>
#else
# define BIT_CAST_ENABLE_IF(TO, FROM)
#endif
#if defined(BIT_CAST_USE_STATIC_ASSERT)
# define BIT_CAST_STATIC_ASSERTS(TO, FROM) do { \
static_assert(sizeof(TO) == sizeof(FROM)); \
static_assert(std::is_trivially_copyable<TO>::value); \
static_assert(std::is_trivially_copyable<FROM>::value); \
} while (false)
#else
# define BIT_CAST_STATIC_ASSERTS(TO, FROM) (void)0
#endif
namespace {
// Defined in header <utility>.
//
// 1. Requires: `sizeof(To) == sizeof(From)`,
// `is_trivially_copyable_v<To>` is `true`,
// `is_trivially_copyable_v<From>` is `true`.
//
// 2. Returns: an object of type `To` whose *object representation* is equal to
// the object representation of `From`.
// If multiple *object representations* could represent the *value
// representation* of `From`, then it is unspecified which `To`
// value is returned.
// If no *value representation* corresponds to `To`'s *object
// representation* then the returned value is unspecified.
template<typename To, typename From BIT_CAST_ENABLE_IF(To, From)>
BIT_CAST_CONCEPTS(TO, FROM)
inline constexpr To bit_cast(const From& from) noexcept {
BIT_CAST_STATIC_ASSERTS(To, From);
typename std::aligned_storage<sizeof(To), alignof(To)>::type storage;
std::memcpy(&storage, &from, sizeof(To)); // Above `constexpr` is optimistic, fails here.
return reinterpret_cast<To&>(storage);
// More common implementation:
// std::remove_const_t<To> to{};
// std::memcpy(&to, &from, sizeof(To)); // Above `constexpr` is optimistic, fails here.
// return to;
}
}
#endif