C++ Move Semantics Cheat Sheet
Explains rvalue references, move constructors and assignment, std::move and std::forward, and the rule of five in modern C++.
3 PagesAdvancedApr 10, 2026
Rvalue References
Bind to temporaries to enable move instead of copy.
cpp
void process(std::string& s) { std::cout << "lvalue ref\n"; }void process(std::string&& s) { std::cout << "rvalue ref\n"; }std::string name = "Ada";process(name); // calls lvalue overloadprocess(std::string("Bob")); // calls rvalue overload (temporary)process(std::move(name)); // calls rvalue overload (name is cast to rvalue)
Move Constructor & Move Assignment
Steal resources from a temporary instead of deep-copying.
cpp
class Buffer {public: Buffer(size_t size) : size(size), data(new int[size]) {} // Move constructor: takes ownership, leaves source in valid empty state Buffer(Buffer&& other) noexcept : size(other.size), data(other.data) { other.size = 0; other.data = nullptr; } // 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; } ~Buffer() { delete[] data; }private: size_t size; int* data;};
std::move and std::forward
Cast to rvalue, and preserve value category in forwarding code.
cpp
#include <utility>std::vector<std::string> v;std::string s = "hello";v.push_back(std::move(s)); // moves s into the vector; s is now unspecified/empty// Perfect forwarding in a generic wrappertemplate <typename T>void wrapper(T&& arg) { inner(std::forward<T>(arg)); // preserves lvalue/rvalue-ness of arg}
Key Terms
Vocabulary for reasoning about value categories and moves.
- lvalue- An expression with identity/an address that persists beyond the expression, e.g. a named variable.
- rvalue- A temporary expression with no persistent identity, e.g. a literal or a function's return-by-value.
- Move Semantics- Transferring ownership of resources from a source object instead of copying them, leaving the source in a valid but unspecified state.
- noexcept on move ops- Marking move constructor/assignment noexcept lets std::vector move elements on reallocation instead of falling back to copy.
- Rule of Five- If you define a destructor, copy ctor, or copy assignment, you should typically define/delete all five: destructor, copy ctor, copy assign, move ctor, move assign.
- Copy Elision / RVO- The compiler constructs the return value directly in the caller's storage, skipping the copy/move entirely (mandatory since C++17 for prvalues).
Pro Tip
Always mark move constructors and move assignment operators noexcept when they truly can't throw - std::vector checks this at compile time and silently falls back to copying during reallocation if the move constructor isn't noexcept, defeating the purpose.
Was this cheat sheet helpful?
Explore Topics
#CMoveSemantics#CMoveSemanticsCheatSheet#Programming#Advanced#RvalueReferences#Move#Constructor#Assignment#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance