Add bit_cast

This commit is contained in:
Victor Zverovich
2018-05-06 08:07:28 -07:00
parent 0adccaefb6
commit a4c7d99f70
2 changed files with 21 additions and 0 deletions

View File

@@ -211,6 +211,17 @@ inline uint32_t clzll(uint64_t x) {
namespace fmt {
namespace internal {
// An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't produce
// undefined behavior (e.g. due to type aliasing).
// Example: uint64_t d = bit_cast<uint64_t>(2.718);
template <typename Dest, typename Source>
inline Dest bit_cast(const Source& source) {
static_assert(sizeof(Dest) == sizeof(Source), "size mismatch");
Dest dest;
std::memcpy(&dest, &source, sizeof(dest));
return dest;
}
// An implementation of begin and end for pre-C++11 compilers such as gcc 4.
template <typename C>
FMT_CONSTEXPR auto begin(const C &c) -> decltype(c.begin()) {