mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-14 03:25:15 +00:00
31 lines
897 B
C++
31 lines
897 B
C++
#pragma once
|
|
|
|
#include <string.h>
|
|
#include <algorithm>
|
|
#include <type_traits>
|
|
|
|
|
|
namespace ext
|
|
{
|
|
/** \brief Returns value `from` converted to type `To` while retaining bit representation.
|
|
* `To` and `From` must satisfy `CopyConstructible`.
|
|
*/
|
|
template <typename To, typename From>
|
|
std::decay_t<To> bit_cast(const From & from)
|
|
{
|
|
To res {};
|
|
memcpy(static_cast<void*>(&res), &from, std::min(sizeof(res), sizeof(from)));
|
|
return res;
|
|
}
|
|
|
|
/** \brief Returns value `from` converted to type `To` while retaining bit representation.
|
|
* `To` and `From` must satisfy `CopyConstructible`.
|
|
*/
|
|
template <typename To, typename From>
|
|
std::decay_t<To> safe_bit_cast(const From & from)
|
|
{
|
|
static_assert(sizeof(To) == sizeof(From), "bit cast on types of different width");
|
|
return bit_cast<To, From>(from);
|
|
}
|
|
}
|