In another question I incorrectly used the term POD to refer to data types that aren't actually POD types (on account of having a constructor). Now, I've looked through the standard and couldn't find a proper name for what I want. Nor can I actually find a guarantee that copying is actually allowed.
The data type I mean is a POD, but may contain functions, including constructors, but nothing that should alter its alignment or size characteristics when compared to an equivalent POD type.
In section 3.9 of the standard it states that POD data can be copied with memcpy, either to another object, or to character data and back. No such guarantee is ever made of non-POD data.
However, the object representation of an object is defined in the same section. It is defined such that one would believe any two objects of the same type could be safely copied via memcpy.
So my questions are:
- Is the copy with memcpy actually guaranteed to be safe for such objects?
- If yes, then why is there a special note about memcpy and POD?
- Is there a name for this type of data which is memcpy safe?
A simple example of the type of object I mean:
struct ex_struct
{
int a,b,c,d;
ex_struct() : a(123) { }
}
Reading the C++0x draft, my struct would appear to be a trivially copyable class (9.1). I believe that implies memcpy would be safe.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…