Move Semantics (C++)
Move semantics let C++ transfer a resource from one object to another instead of copying it, which is faster for large objects. It was introduced in C++11 with rvalue references.
C++
When an object is about to be destroyed (a temporary, or one you explicitly std::move), its contents can be 'stolen' rather than deep-copied. This avoids expensive duplication of things like vector contents.
Classes implement this through a move constructor and move assignment operator. std::move doesn't actually move anything by itself — it just marks a value as eligible to be moved from.
Syntax
auto b = std::move(a);Examples
C++
#include <iostream>
#include <string>
#include <utility>
int main() {
std::string a = "hello";
std::string b = std::move(a);
std::cout << b << std::endl;
return 0;
}Output:
hello