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