Trivially copyable objects can be moved in memory by memmove
, such as:
struct X { };
int main() {
static_assert(std::is_trivially_copyable_v<X>);
X* px1 = (X*)operator new(sizeof(X));
new(px1) X{};
X* px2 = (X*)operator new(sizeof(X));
memmove(px2, px1, sizeof(X));
px2->~X();
operator delete(px1);
operator delete(px2);
}
Live demo: https://wandbox.org/permlink/68d1cqMt6yd8LBR4.
I just wonder whether there are some other mechanisms how to move objects in memory (reallocate them) next to memmove
allowed by the C++ Standards.
To make it clear, I am not asking about C++11 move semantics, which does not move anything by itself.