Week 11 Day 1 - Move Constructors
Today we talked about a very important optimization that C++11 added, which is called "moving".
Suppose you had a vector of size 1B, and you wanted to copy it into another vector, and then delete it. This would have to copy 1B elements, and then call the destructor on 1B elements. C++11 added the ability to detect that, and to do what is called a "Move" instead, where the second vector will simply steal the internals of the first, and so rather than doing two O(N) operations, it does an O(1) steal instead.
When you see a double ampersand (&&) in a constructor or assignment operator (operator =) it means that it is a special function that is designed to steal resources rather than copy. These will be called automatically when C++ can guarantee the right hand side isn't going to be used again, such as the result of function calls, or explicitly by calling std::move() on an object (which turns it into a rvalue).
I rarely call std::move(), though, since optimizers are actually really good at detecting when something can be moved from, so I try to just write clear and readable code and let the optimizer handle it.
If the compiler isn't optimizing it away, then yeah, use std::move() in cases where you know for sure you can steal the resources from the right hand side, and the right side won't be used again (this is just a rule of thumb I use, technically there are some things you can do with an object that has been moved from sometimes).