forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit_cast.h
41 lines (30 loc) · 989 Bytes
/
bit_cast.h
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
#pragma once
#ifdef __cpp_lib_bit_cast
#include <bit>
namespace drake {
namespace internal {
using std::bit_cast;
} // namespace internal
} // namespace drake
#else // __cpp_lib_bit_cast
#include <cstring>
#include <type_traits>
namespace drake {
namespace internal {
/** Implements C++20 https://en.cppreference.com/w/cpp/numeric/bit_cast (but
without the overload resolution guards, which are not necessary in our case.)
(Once all of Drake's supported platforms offer std::bit_cast, we can remove
this function in lieu of the std one.) */
template <class To, class From>
To bit_cast(const From& from) noexcept {
static_assert(std::is_trivially_constructible_v<To>);
To result;
static_assert(sizeof(To) == sizeof(From));
static_assert(std::is_trivially_copyable_v<To>);
static_assert(std::is_trivially_copyable_v<From>);
std::memcpy(&result, &from, sizeof(result));
return result;
}
} // namespace internal
} // namespace drake
#endif // __cpp_lib_bit_cast