提交 8c9aa373 编写于 作者: A Alexey Milovidov

Removed useless code [#CLICKHOUSE-2].

上级 7412167c
#pragma once
#include <string>
#include <type_traits>
namespace ext
{
template <typename To, typename From>
union bit_cast_helper_t
{
using dst = std::decay_t<To>;
using src = std::decay_t<From>;
static_assert(std::is_copy_constructible<dst>::value, "destination type must be CopyConstructible");
static_assert(std::is_copy_constructible<src>::value, "source type must be CopyConstructible");
/** value-initialize member `to` first in case destination type is larger than source */
dst to{};
src from;
bit_cast_helper_t(const From & from) : from{from} {}
operator dst() const { return to; }
};
/** \brief Returns value `from` converted to type `To` while retaining bit representation.
* `To` and `From` must satisfy `CopyConstructible`. */
* `To` and `From` must satisfy `CopyConstructible`.
*/
template <typename To, typename From>
std::decay_t<To> bit_cast(const From & from)
{
return bit_cast_helper_t<To, From>{from};
To res {};
memcpy(&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`. */
* `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_helper_t<To, From>{from};
return bit_cast<To, From>(from);
};
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册