Lesson 3 of 9 · 25m
Move Semantics and Honest Value Types
Understand moves as ownership transfer, design types that behave like values, and avoid the false economy of micro-managing copies.
Before move semantics, C++ programmers wrote awkward output parameters and factory functions partly to avoid copies. Then they wrote clever copy elision folklore on top. Modern C++ is cleaner: values can be returned directly, ownership can transfer cheaply, and types that manage resources must state whether they copy, move, or neither. The trick is learning what std::move actually does — because it does not move anything.
Move means transfer, not magic
std::move(x) is only a cast to an rvalue reference. It gives a constructor or assignment operator permission to steal x's representation. The actual move happens in that operation.
std::string payload = build_payload();
std::string wire = std::move(payload); // payload is now valid but unspecifiedAfter the move, payload is not empty by language decree. It is in a valid but unspecified state. You may destroy it, assign to it, or call operations with no preconditions. Do not depend on its contents.
Value semantics make APIs boring in a good way
Prefer types that can be copied, moved, compared, and reasoned about independently:
struct UserName {
std::string value;
};
UserName normalize(UserName input) {
trim(input.value);
lowercase(input.value);
return input;
}This function takes by value, mutates locally, and returns the result. It is simple because ownership is unambiguous. For small types, pass by value can be wasteful; for types you intend to consume, it is often the clearest interface. Let the optimizer handle copies when the object is cheap; use measurement before contorting the API.
Rule of zero first
The best resource-managing class often implements no destructor, copy operations, or move operations because its members already do the right thing:
struct Worker {
std::string name;
std::vector<std::byte> buffer;
std::unique_ptr<Connection> connection;
};This type is movable but not copyable because unique_ptr is movable but not copyable. That is the rule of zero working. Write destructor/copy/move yourself only when the class directly owns a raw resource. If you write one of the five special members, consider all five; inconsistent copying is a classic source of double-free and accidental deep copies.
Moves matter in containers
When a std::vector grows, it relocates elements. If the move constructor is noexcept, vector can move elements efficiently. If moving might throw, vector may copy instead to preserve the strong exception guarantee:
struct Buffer {
Buffer(Buffer&&) noexcept = default;
Buffer& operator=(Buffer&&) noexcept = default;
};Do not slap noexcept on operations that can actually throw. Mark them noexcept when you can uphold it, because containers and optimizers make decisions based on that promise.
Do not move when you need the source
This sounds obvious until async code enters the picture. A lambda capturing a moved object, a loop moving the same accumulator, or a moved object reused after an early branch are all common review findings. Moves also do not automatically clear input parameters unless the type implements that behavior.
Field notes
- Returning a local object does not need
std::move; copying is normally elided or moved automatically. Explicitstd::movecan prevent copy elision. std::moveon aconstobject usually produces a copy because the move constructor cannot accept it as a non-const rvalue.- Moved-from objects must still be destructible and assignable, even if their value is unspecified.
- If a type is expensive to copy and cheap to move, deleting copy operations often makes accidental performance bugs visible at compile time.
Was this lesson helpful?