So, when is the compiler allowed to move objects instead of copying? As a short answer, the compiler moves an object when the object can be categorized as an r-value. The term r-value might sound complicated, but in essence it is just an object which is not tied to a named variable, for either of the following reasons:
- It's coming straight out of a function
- We make a variable an r-value by using std::move(...)
The following example demonstrates both of these scenarios:
// Below, the object coming out of make_buffer is not tied to a variable// Therefore moved to x auto x = make_buffer(); // Below, "x" is passed into std::move(...)// Therefore move-assigned to y auto y = std::move(x);
Let's make this a little ...