cp-book

ecnerwala's competitive programming library

View the Project on GitHub ecnerwala/cp-book

:warning: #include "bit_cast.hpp"

View this file on GitHub · Last update: 2020-09-13 20:50:00-07:00

References:

Code

Coverage Exec / Excl / Total
Lines 0.0% 0 / 0 / 4
Full report
#pragma once

#include <type_traits>
#include <cstring>

// Copied from https://en.cppreference.com/w/cpp/numeric/bit_cast

template <class To, class From>
typename std::enable_if_t<
	sizeof(To) == sizeof(From) &&
	std::is_trivially_copyable_v<From> &&
	std::is_trivially_copyable_v<To>,
	To>
// constexpr support needs compiler magic
bit_cast(const From& src) noexcept
{
	static_assert(std::is_trivially_constructible_v<To>,
		"This implementation additionally requires destination type to be trivially constructible");

	To dst;
	std::memcpy(&dst, &src, sizeof(To));
	return dst;
}
#include <type_traits>
#include <cstring>
#line 2 "src/bit_cast.hpp"

#line 5 "src/bit_cast.hpp"

// Copied from https://en.cppreference.com/w/cpp/numeric/bit_cast

template <class To, class From>
typename std::enable_if_t<
	sizeof(To) == sizeof(From) &&
	std::is_trivially_copyable_v<From> &&
	std::is_trivially_copyable_v<To>,
	To>
// constexpr support needs compiler magic
bit_cast(const From& src) noexcept
{
	static_assert(std::is_trivially_constructible_v<To>,
		"This implementation additionally requires destination type to be trivially constructible");

	To dst;
	std::memcpy(&dst, &src, sizeof(To));
	return dst;
}
// clang-format off
// @formatter:off
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wmisleading-indentation"
#pragma GCC diagnostic ignored "-Wmultistatement-macros"
#include <bits/stdc++.h>
// src/bit_cast.hpp
template<class To,class From>
typename std::enable_if_t<
sizeof(To)==sizeof(From)&&
std::is_trivially_copyable_v<From>&&
std::is_trivially_copyable_v<To>,
To>
bit_cast(const From&src)noexcept
{
static_assert(std::is_trivially_constructible_v<To>,
"This implementation additionally requires destination type to be trivially constructible");
To dst;
std::memcpy(&dst,&src,sizeof(To));
return dst;
}
#pragma GCC diagnostic pop
// clang-format on
// @formatter:on
#pragma once

#include <type_traits>
#include <cstring>

// Copied from https://en.cppreference.com/w/cpp/numeric/bit_cast

template <class To, class From>
typename std::enable_if_t<
	sizeof(To) == sizeof(From) &&
	std::is_trivially_copyable_v<From> &&
	std::is_trivially_copyable_v<To>,
	To>
// constexpr support needs compiler magic
bit_cast(const From& src) noexcept
{
	static_assert(std::is_trivially_constructible_v<To>,
		"This implementation additionally requires destination type to be trivially constructible");

	To dst;
	std::memcpy(&dst, &src, sizeof(To));
	return dst;
}
Back to top page