Skip to content

Commit

Permalink
Fix a violation of C++ standard rules that unions cannot be switched.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheQuantumPhysicist authored and furszy committed Jul 3, 2021
1 parent d6380c4 commit 806213a
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm>
#include <array>
#include <assert.h>
#include <cstring>
#include <ios>
#include <limits>
#include <list>
Expand Down Expand Up @@ -137,27 +138,27 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
}
inline uint64_t ser_double_to_uint64(double x)
{
union { double x; uint64_t y; } tmp;
tmp.x = x;
return tmp.y;
uint64_t tmp;
std::memcpy(&tmp, &x, sizeof(x));
return tmp;
}
inline uint32_t ser_float_to_uint32(float x)
{
union { float x; uint32_t y; } tmp;
tmp.x = x;
return tmp.y;
uint32_t tmp;
std::memcpy(&tmp, &x, sizeof(x));
return tmp;
}
inline double ser_uint64_to_double(uint64_t y)
{
union { double x; uint64_t y; } tmp;
tmp.y = y;
return tmp.x;
double tmp;
std::memcpy(&tmp, &y, sizeof(y));
return tmp;
}
inline float ser_uint32_to_float(uint32_t y)
{
union { float x; uint32_t y; } tmp;
tmp.y = y;
return tmp.x;
float tmp;
std::memcpy(&tmp, &y, sizeof(y));
return tmp;
}


Expand Down

0 comments on commit 806213a

Please sign in to comment.