Move semantics and std::move
What moving means in C++, rvalue references, what std::move really does, and the rule of five.
Copying a std::vector of a million elements copies a million elements. Moving it just hands over the internal buffer pointer, leaving the source empty. That's why returning big objects by value is cheap in modern C++.
A class supports moving with a move constructor T(T &&other) noexcept and move assignment. std::move(x) doesn't move anything itself: it's a cast that says "you may steal from x". After a move, only assign to or destroy the source.
Mark move operations noexcept, or std::vector will copy instead of move when it grows.
Example
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int main() {
std::string big(1000, 'x');
std::vector<std::string> v;
v.push_back(std::move(big));
std::cout << v[0].size() << " " << big.empty() << "\n";
return 0;
}
Output:
1000 1
Watch it run: Moving instead of copying