Suppose that T is a POD type that doesn't contain a pointer, and I want to serialize T (in addition to some other data too). I've created the below functions to do this:
template<class T> void serialize(const T& source, char*& dest)
{
*(T*)dest = source;
dest += sizeof(T);
}
template<class T> void deserialize(T& dest, char*& source)
{
dest = *(T*)source;
source += sizeof(T);
}
Will this cause any problems, or are there any compilers where this won't work? In other words, will the code:
template<class T> bool check_sanity(const T& obj)
{
std::unique_ptr<char[]> buffer { new int[sizeof(T)] };
serialize(obj, buffer);
T new_obj;
deserialize(new_obj, buffer);
return new_obj == obj;
}
Ever return false? (Assume T is POD and no one's overloaded the == operator).
I'm writing these serialization methods for use in conjunction with MPI, where they'll be used at the start of the program to distribute some of the data needed for bookkeeping, so the same program will always be serializing and deserializing data.