Suppose I have this function:
template<class T>
uint8_t* toBytes(T&& obj)
{
uint8_t* array = new uint8_t[sizeof(T)];
for (int x = 0; x < sizeof(T); x++)
{
array[x] = reinterpret_cast<uint8_t*>(&obj)[x];
}
return array;
}
I am fairly certain that this is defined behavior (as long as don't expect the memory to look like any thing specific ... I think).
But now suppose I have another function:
template<class T>
T* toType(uint8_t* array)
{
return reinterpret_cast<T*>(array);
}
Is the following defined?
class A { /* Members of A */ };
A a;
uint8_t array = toBytes(a);
A* anotherA = toType<A>(array);