If you can use bitwise copying or not, this has nothing to do with the struct or class tag and depends only on whether the struct or class is_trivially_copiable . Are they defined in the standard (9/6 [class]), and basically it comes down to the fact that you don't need to declare any other special member methods than the constructors.
The bitwise copy is then permitted by the standard of 3.9 / 2 [basic.types]
For any object (except a subobject of the base class) of a trivially copied type T , regardless of whether the object has a valid value of type T , the basic bytes (1.7) that make up the object can be copied to a char or unsigned char array. If the contents of a char or unsigned char array are copied back to the object, the object subsequently retains its original value. [Example:
#define N sizeof(T) char buf[N]; T obj;
-end example]
Note: a bitwise copy of the fill bytes will result in reports in Valgrind.
Using std::copy with the same effect:
char const* b = reinterpret_cast<char const*>(&obj); std::copy(b, b + N, buf);
source share