bit_cast.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <type_traits> | ||
| 4 | #include <cstring> | ||
| 5 | |||
| 6 | namespace wala { | ||
| 7 | |||
| 8 | // Copied from https://en.cppreference.com/w/cpp/numeric/bit_cast | ||
| 9 | |||
| 10 | template <class To, class From> | ||
| 11 | ✗ | typename std::enable_if_t< | |
| 12 | sizeof(To) == sizeof(From) && | ||
| 13 | std::is_trivially_copyable_v<From> && | ||
| 14 | std::is_trivially_copyable_v<To>, | ||
| 15 | To> | ||
| 16 | // constexpr support needs compiler magic | ||
| 17 | bit_cast(const From& src) noexcept | ||
| 18 | { | ||
| 19 | static_assert(std::is_trivially_constructible_v<To>, | ||
| 20 | "This implementation additionally requires destination type to be trivially constructible"); | ||
| 21 | |||
| 22 | ✗ | To dst; | |
| 23 | ✗ | std::memcpy(&dst, &src, sizeof(To)); | |
| 24 | ✗ | return dst; | |
| 25 | } | ||
| 26 | |||
| 27 | } // namespace wala | ||
| 28 |