You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
662 B
21 lines
662 B
#pragma once
|
|
|
|
#include <cstring>
|
|
#include <memory>
|
|
|
|
template <typename To>
|
|
struct bit_cast_fn {
|
|
template <typename From>
|
|
constexpr auto operator()(const From &source) const noexcept -> std::
|
|
enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> && std::is_trivially_copyable_v<To>, To>
|
|
{
|
|
static_assert(std::is_trivially_constructible_v<To>, "Input type must be trivially constructible");
|
|
|
|
To destination;
|
|
std::memcpy(std::addressof(destination), std::addressof(source), sizeof(To));
|
|
return destination;
|
|
}
|
|
};
|
|
|
|
template <typename To>
|
|
static constexpr auto bit_cast = bit_cast_fn<To>{};
|