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.
memcpy
that it doesn't have requirements on the types? It is the only way in C++ to safely perform type-punning. – Protmemcpy
has fairly strict requirements on the types involved, or else it will do damage. Consider any type a user-defined copy constructor: obviously just copying the bytes over is unlikely to do what the copy constructor would have done. – Crarymemcpy
is supposed to do. It literally copies memory. – Protstd::memcpy
is not allowed regardless of anything else. – Crary