If the standard state is defined only for POD types (I have not studied the C ++ 11 standard yet, but I don’t know if your statement is correct or not (a) ), and you do it for a non-POD type, this is not defined behavior . Period.
It can work on some implementations in some environments at certain times of the day when the planets are aligned. It can work the vast majority of times. This still does not make a good idea if you value portability.
(a) After a more thorough investigation, your specific case seems to be in order. Section 3.9 / 3 of the standard ( n3242 draft , but I would be surprised if it changed a lot from this late draft):
For any trivially copied type T, if two pointers to T point to different T-objects obj1 and obj2, where neither obj1 nor obj2 are a subobject of the base class, if the base bytes that make up obj1 are copied to obj2, obj2 then save then same value as obj1.
Section 9 defines (at a high level) what “trivially copied” means:
A trivially copied class is a class that: - does not have non-trivial copy constructors (12.8),
- there are no non-trivial displacement constructors (12.8),
- does not have non-trivial copy assignment operators (13.5.3, 12.8),
- there are no nontrivial displacement assignment operators (13.5.3, 12.8) and
- has a trivial destructor (12.4).
when these sections will be discussed in more detail in each area, 12.8 for copying and moving class objects and 13.5.3 for assignments.
source share