mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-25 15:21:54 +00:00
Add bit_cast
This commit is contained in:
parent
0adccaefb6
commit
a4c7d99f70
@ -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()) {
|
||||
|
@ -397,6 +397,16 @@ TEST(FixedBufferTest, BufferOverflow) {
|
||||
EXPECT_THROW_MSG(buffer.resize(11), std::runtime_error, "buffer overflow");
|
||||
}
|
||||
|
||||
TEST(UtilTest, BitCast) {
|
||||
struct S {
|
||||
uint32_t u[2];
|
||||
};
|
||||
auto s = fmt::internal::bit_cast<S>(uint64_t(42));
|
||||
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), 42);
|
||||
s = fmt::internal::bit_cast<S>(uint64_t(~0ull));
|
||||
EXPECT_EQ(fmt::internal::bit_cast<uint64_t>(s), ~0ull);
|
||||
}
|
||||
|
||||
TEST(UtilTest, Increment) {
|
||||
char s[10] = "123";
|
||||
increment(s);
|
||||
|
Loading…
Reference in New Issue
Block a user