std::move
Use case
Transfer ownership of data without copying.
Explanation
std::move casts an lvalue to an rvalue reference (enabling the move constructor/assignment). The data is transferred, not copied (the original vector is left empty).
Code
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> instructions = {"stp x29, x30, [sp, #-16]!",
"mov x29, sp", "ret"};
std::cout << "Before move: " << instructions.size() << " instructions.\n";
std::vector<std::string> cache = std::move(instructions);
std::cout << "After move:\n";
std::cout << " original: " << instructions.size() << "\n";
std::cout << " cache: " << cache.size() << "\n";
return 0;
}
Output
$ ./src/c++11/build/std-move
Before move: 3 instructions.
After move:
original: 0
cache: 3