Motivation and use of move constructors in C++
- by Giorgio
I recently have been reading about move constructors in C++ (see e.g. here) and I am trying to understand how they work and when
I should use them.
As far as I understand, a move constructor is used to alleviate the performance problems caused by copying large objects. The wikipedia page says: "A chronic performance problem with C++03 is the costly and unnecessary deep copies that can happen implicitly when objects are passed by value."
I normally address such situations
by passing the objects by reference, or
by using smart pointers (e.g. boost::shared_ptr) to pass around the object (the smart pointers get copied instead of the object).
What are the situations in which the above two techniques are not sufficient
and using a move constructor is more convenient?