Add some safety static_asserts to remote read/write functions

This commit is contained in:
Maxime Coste 2018-04-07 12:32:31 +10:00
parent 4ff0c58518
commit 23853f499e

View File

@ -67,6 +67,7 @@ public:
template<typename T> template<typename T>
void write(const T& val) void write(const T& val)
{ {
static_assert(std::is_trivially_copyable<T>::value, "");
write((const char*)&val, sizeof(val)); write((const char*)&val, sizeof(val));
} }
@ -196,15 +197,10 @@ public:
template<typename T> template<typename T>
T read() T read()
{ {
union U static_assert(std::is_trivially_copyable<T>::value, "");
{ T res;
T object; read(reinterpret_cast<char*>(&res), sizeof(T));
alignas(T) char data[sizeof(T)]; return res;
U() {}
~U() { object.~T(); }
} u;
read(u.data, sizeof(T));
return u.object;
} }
template<typename T> template<typename T>