How to move an object into uninitialized memory?

c++, c++11, move-semantics

Solution

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();

Problem

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?

Original source

Related problems