Rvalue References in C++: Move Semantics


This post and the examples are from the Microsoft C++ Documentation.

The rvalue references enable us to distinguish an lvalue from an rvalue (see this post for more detail about value categories). In this post, we show how rvalue references support the implementation of move semantics.

Move Semantics

Rvalue references support the implementation of move semantics. Move semantics enable us to write code that transfers resources (such as dynamically allocated memory) from one object to another. To implement move semantics, we can provide a move constructor to a class, which is not provided by the compiler. Copy and assignment operations whose sources are rvalues then automatically take advantage of move semantics.

Consider the following example:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string s = string("h") + "e" + "ll" + "o";
   cout << s << endl;
}

In this example, operator + cannot append one string to the other because it does not know whether the source strings are lvalues or rvalues. If the source strings are both lvalues, they might be referenced elsewhere in the program and therefore must not be modified. By using rvalue references, operator + can be modified to take rvalues, which cannot be referenced elsewhere in the program. Therefore, operator + can now append one string to another. This can significantly reduce the number of dynamic memory allocations that the string class must perform.




Related Posts

Rvalue References in C++: Perfect Forwarding

Perfect forwarding reduces the need for overloaded functions and helps...

Rvalue References in C++: Move Semantics

The rvalue references enable us to distinguish an lvalue from...

References in C++

A reference is an alias of another object. It stores...

Lvalue References in C++

An lvalue reference is another name of an object. An...