I'm not sure that strictly speaking the standard does allow you to memcpy
into an array of char
and back again, although you'd certainly get away with it with PODs.
But things get murkier when you start looking at all the complicated things C++ can do. Consider the following:
struct ex1;
struct ex2
{
ex2(std::unique_ptr<ex1> ptr) : member{ptr} {}
private:
std::unique_ptr<ex1> member;
};
The struct ex2
is move-only because it has a move-only member variable. So what happens if we use memcpy
to construct a bit-for-bit identical copy of an ex2
instance? We end up with two objects which both think they own the member pointer. What happens when the second one of these gets deleted? You get the idea.