Move Semantics
Move semantics let C++ shuffle resources between objects without copying a byte of payload. For large objects, that's the difference between a snappy program and a sluggish one.
The Problem with Copying
std::vector<int> create_large_vector() { std::vector<int> v(1000000, 42); return v; // no copy here: elided, or moved at worst}Compilers have been allowed to skip that copy entirely (copy elision, or NRVO for a named local) since C++98, and in practice they usually do. But when elision didn't apply, a pre-C++11 compiler had no choice except to copy all million integers. Move semantics close that gap, and they cover the cases elision can't touch, like handing a vector you've finished with to another object.
Lvalues and Rvalues
- Lvalue: has a name and persists beyond the current expression (
int x = 5;-xis an lvalue) - Rvalue: a temporary value that is about to be destroyed (
5,x + y,create_vector())
Move Constructor and Move Assignment
class Buffer { int *data; size_t size;public: // Move constructor: steal from other Buffer(Buffer &&other) noexcept : data(other.data), size(other.size) { other.data = nullptr; other.size = 0; }
// Move assignment Buffer &operator=(Buffer &&other) noexcept { if (this != &other) { delete[] data; data = other.data; size = other.size; other.data = nullptr; other.size = 0; } return *this; }};A move hands ownership of a resource (heap memory, file handle, mutex, you name it) from one object to another. The source is left in a valid-but-unspecified state. Usually empty, sometimes nulled out, but always safe to destroy. Deep copies disappear from the hot path.
std::move
std::move is just a cast from lvalue to rvalue reference. That cast is what unlocks the move:
std::string a = "hello";std::string b = std::move(a); // b owns "hello"; a is valid but unspecified (empty in practice)Despite the name, std::move doesn't actually move anything. It only changes the type so overload resolution picks the move constructor or move assignment operator, where the real work happens.
A type that prints on every copy and move makes all of this visible. The runner has no compiler flags, so you can't switch elision off with -fno-elide-constructors; look for what make() doesn't print instead. The last section shows why move constructors should be noexcept: a growing std::vector only moves its old elements if the move can't throw.
When Moves Happen Automatically
The compiler uses move semantics automatically when:
- Returning a local variable from a function (when NRVO doesn't remove the copy outright). So don't write
return std::move(local);: it turns the local into an expression NRVO can't elide, forcing a move where there might have been nothing at all - Passing a temporary to a function
- Inserting a temporary into a container
std::vector<std::string> v;v.push_back(std::string("hello")); // temporary is moved, not copiedv.push_back("world"); // also moved (implicit construction)Perfect Forwarding
In templates, std::forward preserves whether an argument was an lvalue or rvalue:
template <typename T>void wrapper(T &&arg) { inner_function(std::forward<T>(arg));}Factory functions like std::make_unique rely on this trick to ferry constructor arguments through without spurious copies.
The Performance Impact
Move semantics quietly collapsed a lot of O(n) copies into O(1) pointer swaps:
| Operation | Before moves | After moves |
|---|---|---|
| Return a vector (when not elided) | Copy all elements | Move pointer |
| Insert string into map | Copy characters | Move pointer |
| Swap two vectors | Copy all elements | Swap three pointers |