Move semanticssuggest change
Move semantics are a way of moving one object to another in C++. For this, we empty the old object and place everything it had in the new object.
For this, we must understand what an rvalue reference is. An rvalue reference (
T&& where T is the object type) is not much different than a normal reference (
T&, now called lvalue references). But they act as 2 different types, and so, we can make constructors or functions that take one type or the other, which will be necessary when dealing with move semantics.
The reason why we need two different types is to specify two different behaviors. Lvalue reference constructors are related to copying, while rvalue reference constructors are related to moving.
To move an object, we will use
std::move(obj). This function returns an rvalue reference to the object, so that we can steal the data from that object into a new one. There are several ways of doing this which are discussed below.
Important to note is that the use of
std::move creates just an rvalue reference. In other words the statement
std::move(obj) does not change the content of obj, while
auto obj2 = std::move(obj) (possibly) does.