Is it always safe to std::memmove
between the same object instances (including subobjects).
That is, is the following safe for any T
and any t
, and will it leave t
unchanged:
template <typename T>
void maybe_copy(T& t) {
std::memmove(&t, &t, sizeof(T));
}
As a corollary question, is the following always safe:
template <typename T>
void redundant_copy(T& s, const T& d) {
if (std::addressof(s) == std::addressof(d)) {
std::memmove(&d, &s, sizeof(T));
}
}