Under what set of conditions is it safe to use std::memcpy
to copy from one object to another?
For example, what conditions must T
, src
and dest
satisfy for the following to be safe:
template <typename T>
void copy_bytewise(T& dest, const T& src) {
std::memcpy(&dest, &src, sizeof(T));
}
The only thing we can assume about src
and dest
is that they don't overlap1. In particular either of src
or dest
may be a reference to a member or base class.
I am interested in answers which refer to the standard, but if this diverges from common practice (e.g., the de-facto C++ ABI from Itanium) I'd also like to know.
Note that T
satisfying the TriviallyCopyable (TC) concept is not sufficient, as this example shows. base
is TC yet not memcpy-safe (due to re-use of padding for members of a derived class).
I am especially interested if there is any condition on T
alone that is sufficient (and not necessarily necessary), without requiring conditions on src
and dest
(that cannot, in general, be statically determined).
1 Specifically, my assumption is that if they do overlap, they are still safe to copy under the same conditions on T
as for std::memcpy
, but using std::memmove
instead. If assumption is incorrect, it could be part of an answer.