Move Semantics
Right values, move constructors, std::move, and perfect forwarding.
Key Pointsstd::move is a cast that enables move; a moved-from object is valid but unspecified.
C++
#include <string>
#include <utility>
#include <vector>
std::vector<std::string> v;
std::string big(1'000'000, 'x');
v.push_back(big); // copy: expensive
v.push_back(std::move(big)); // move: steals the buffer, O(1)Follow the rule of five: if you define one of dtor/copy/move, define all five.
C++
#include <string>
#include <utility>
#include <vector>
std::vector<std::string> v;
std::string big(1'000'000, 'x');
v.push_back(big); // copy: expensive
v.push_back(std::move(big)); // move: steals the buffer, O(1)遵循五法则:定义了析构/拷贝/移动中的任一个,就把五个全部定义。