Given an allocated but uninitialized memory location, how do I move some object into that location (destroying the original), without constructing potentially expensive intermediate objects?
You could use placement new to move-construct it in the memory:
void * memory = get_some_memory();
Thing * new_thing = new (memory) Thing(std::move(old_thing));
If it has a non-trivial destructor, then you'll need to explicitly destroy it when you're done:
new_thing->~Thing();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With