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() <...