#ifndef BASE_BIT_CAST_H_
#define BASE_BIT_CAST_H_
#include <type_traits>
namespace base {
template <class Dest, class Source>
constexpr Dest bit_cast(const Source& source) {
static_assert(!std::is_pointer_v<Source>,
"bit_cast must not be used on pointer types");
static_assert(!std::is_pointer_v<Dest>,
"bit_cast must not be used on pointer types");
static_assert(!std::is_reference_v<Source>,
"bit_cast must not be used on reference types");
static_assert(!std::is_reference_v<Dest>,
"bit_cast must not be used on reference types");
static_assert(
sizeof(Dest) == sizeof(Source),
"bit_cast requires source and destination types to be the same size");
static_assert(std::is_trivially_copyable_v<Source>,
"bit_cast requires the source type to be trivially copyable");
static_assert(
std::is_trivially_copyable_v<Dest>,
"bit_cast requires the destination type to be trivially copyable");
return __builtin_bit_cast(Dest, source);
}
}
#endif