We know copies are expensive, especially heavy objects. The move semantics that were introduced in C++11 help us avoid expensive copies. The foundational concept behind std::move and std::forward is the rvalue reference. This recipe will show you how to use std::move.
Learning how move semantics works
How to do it...
Let's develop three programs to learn about std::move and its universal reference:
- Let's start by developing a simple program:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> a = {1, 2, 3, 4, 5};
auto b = std::move(a);
std::cout << "a: " << a.size() << std::endl;
std::cout << "b: " << b.size() << std::endl;
}
- Let's develop a second example:
#include <iostream>
#include <vector>
void print (std::string &&s)
{
std::cout << "print (std::string &&s)" << std::endl;
std::string str (std::move(s));
std::cout << "universal reference ==> str = " << str
<< std::endl;
std::cout << "universal reference ==> s = " << s << std::endl;
}
void print (std::string &s)
{
std::cout << "print (std::string &s)" << std::endl;
}
int main()
{
std::string str ("this is a string");
print (str);
std::cout << "==> str = " << str << std::endl;
return 0;
}
- Let's look at an example with the universal reference:
#include <iostream>
void print (std::string &&s)
{
std::cout << "print (std::string &&s)" << std::endl;
std::string str (std::move(s));
std::cout << "universal reference ==> str = " << str
<< std::endl;
std::cout << "universal reference ==> s = " << s << std::endl;
}
void print (std::string &s)
{
std::cout << "print (std::string &s)" << std::endl;
}
int main()
{
print ("this is a string");
return 0;
}
The next section will describe these three programs in detail.
How it works...
The output of the first program is as follows (g++ move_01.cpp and ./a.out):

In this program, auto b = std::move(a); does a couple of things:
- It casts the vector, a, to the rvalue reference.
- As it is an rvalue reference, the vector move constructor is called, which moves the content of the a vector to the b vector.
- a doesn't have the original data anymore, b has.
The output of the second program is as follows (g++ moveSemantics2.cpp and ./a.out):

In this second example, the str string we pass to the print method is an lvalue reference (that is, we can take the address of that variable), so it is passed by reference.
The output of the third program is as follows (g++ moveSemantics3.cpp and ./a.out):

In the third example, the method that's being called is the one with the universal reference as a parameter: print (std::string &&s). This is because we cannot take the address of this is a string, which means it is an rvalue reference.
It should be clear now that std::move doesn't actually move anything – it is a function template that performs an unconditional cast to an rvalue, as we saw in the first example. This allows us to move (and not copy) the data to the destination and invalidate the source. The benefits of std::move are huge, especially every time we see an rvalue reference parameter to a method (T&&) that would probably* be a copy in the previous versions of the language (C++98 and before).
*Probably: it depends on compiler optimizations.
There's more...
std::forward is somewhat similar (but with a different purpose). It is a conditional cast to an rvalue reference. You are invited to learn more about std::forward, rvalue, and lvalue by reading the books referenced in the next section.
See also
The books Effective Modern C++ by Scott Meyers and The C++ Programming Language by Bjarne Stroustrup cover these topics in great detail.