2012-04-18 1 CPlusPlus11

Move semantics

Copying big objects has always been a problem and somethings that you want to avoid. Things you have to think about is using reference to const in parameter so you don't get expensive copies. Smart pointer with reference counting is another way to avoid copying. Smart compilers can also avoid some copying that can be optimized away. One example of this is the return value optimization were the temporary object returned from a function is used directly, without copying, to initialize the object holding the value after the function call.

Still there are more temporaries to be avoided and the thing called move semantics or rvalue references are here to achieve that. It seems easy to explain but since much of this with temporaries are, if not hidden to the user, so not as obvious at least, and not what you normally think a lot about when solving coding problems. So we are probably up to one or more surprises here as we go along and I'm afraid that this is probably not a silver bullet for avoiding needless copies.

I'll come back with some code examples shortly.