tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bit_cast.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_BIT_BIT_CAST_HPP
4#define TETL_BIT_BIT_CAST_HPP
5
6#include <etl/_config/all.hpp>
7
12
13namespace etl {
14
15namespace detail {
16
17template <typename To, typename From>
18concept bitcastable = (sizeof(To) == sizeof(From)) and is_trivially_copyable_v<From> and is_trivially_copyable_v<To>;
19
20} // namespace detail
21
36template <typename To, typename From>
37 requires detail::bitcastable<To, From>
38constexpr auto bit_cast(From const& src) noexcept -> To
39{
40#if __has_builtin(__builtin_bit_cast) or (defined(_MSC_VER) and not defined(__clang__))
41 return __builtin_bit_cast(To, src);
42#else
43 To dst{};
44 etl::detail::memcpy<char, etl::size_t>(&dst, &src, sizeof(To));
45 return dst;
46#endif
47}
48
49} // namespace etl
50
51#endif // TETL_BIT_BIT_CAST_HPP
constexpr auto bit_cast(From const &src) noexcept -> To
Obtain a value of type To by reinterpreting the object representation of from. Every bit in the value...
Definition bit_cast.hpp:38
Definition adjacent_find.hpp:8
constexpr bool is_trivially_copyable_v
Definition is_trivially_copyable.hpp:21