In object oriented designs, there are times when you replace an object by another. Example: when you store an object into an STL container. In a lot of those cases, you create an object, and add it. The object in the container is a copy of the object that you want to store. And then you discard the original object:
my_insurrance_array[2] = insurance("car");
In essence, it's a move. The object replaces the existing object in the array, and the temporary object is discarded. Standard, C++ does this by copying the value of each non-static data member from the temporary object into those of the existing object in that array. And for classes that have small data members (numbers, bools, ...) this is the fastest solution.
But once your class has larger data members, such as strings, containers, it becomes more expensive (for memory and clock ticks) to copy all that data over. To deal with that, C++ has introduced a move constructor, and the move assignment operator.
T::T(T&& rhd) noexcept : value(rhd.value), s(std::move(rhd.s)) { // move contructor
cout << "move ctor" << endl;
}
T& T::operator=(T&& rhd) noexcept { // move assignment operator
value = rhd.value;
s = std::move(rhd.s);
cout << "move =" << endl;
return *this;
}
move constructor
If you construct a new object from an existing one, and that original one goes out of scope, C++ will select the move constructor. Here's an example of code that can (and will) invoke the move constructor:
T move_to(T t) {
return t;
}
int main() {
// ...
T u = move_to(T());
This function receives an object as pass-by-value. So it's an object on its own. Then we return it to a calling application, where it will be received into a variable.
What C++ does, if you have a move constructor: it will use that constructor to create the object in place in the receiving variable. And use your move code to move the string member from the parameter object to the new object. You "transfer the ownership of that string member from parameter t to the newly constructed object. The parameter object is then destructed.
move assignment operator
If you move an object into the place of another, C++ can (and will, if you provided it) call the move assignment operator. And use it's capability to transfer ownership of expensive data members to the receiving object. An example is, to assign an object to a particular location in an stl array:
std::array<T, 4> array_of_t;
array_of_t[0] = T();
The temporary object T() is moved into the container. The object at array_of_t[0] gets the ownership of T().s. Without copying the data over. That T() object is then destructed.
takeaway: my examples are somewhat artificial, but that's often the case when you try to show a mechanism. But the summary is: if you have an object with expensive data members, it makes sense to provide a move constructor and a move assignment operator. Whenever the compiler has the possibility, it 'll use those to transfer an existing object to a new location. And do that cheaper as when you only have a (default) copy constructor.
full code of blog 1 and 2:
#include <array> #include <iostream> using std::cout; using std::endl; struct T { int value; std::string s; T(); // ctor ~T(); // dtor T(T&&) noexcept; // move ctor T(T const&); // copy ctor T& operator=(T&&) noexcept; // move assignment T& operator=(T const&); // copy assignment }; T::T() : value(0), s("this is an attribute that's expensive to copy") { cout << "ctor" << endl; } T::~T() { cout << "dtor" << endl; } T::T(T&& rhd) noexcept : value(rhd.value), s(std::move(rhd.s)) { cout << "move ctor" << endl; } T::T(T const& rhd) { value = rhd.value; s = rhd.s; cout << "copy ctor" << endl; } T& T::operator=(T&& rhd) noexcept { value = rhd.value; s = std::move(rhd.s); cout << "move =" << endl; return *this; } T& T::operator=(T const& rhd) { value = rhd.value; cout << "copy =" << endl; return *this; } T work() { return T(); } void use() { auto obj = work(); } T move_to(T t) { return t; } int main() { T t = work(); T v(); T u = move_to(T()); T w = move_to(u); T z = std::move(t); // move-constructs from xvalue z = u; use(); std::array<T, 4> array_of_t; array_of_t[0] = T(); return 0; }
Inspiration: MC++: The Rule of Zero, or Six and artificial::mind blog: Moves in Returns, cppreference Move constructors.
Thank you for reading.
C++ moving and returning objects part 1: object as return value (embedded friendly C++)
C++ moving and returning objects part 2: move and copy (embedded friendly C++)