Advanced
minutes
Move Semantics Terminology
Overview of move semantics and resource management concepts in this chapter.
Last updated:
Move Semantics Terminology Reference
This reference provides an overview of move semantics terminology you'll encounter in this chapter. Use it as a quick lookup guide.
Value Categories
| Term | Definition | Example |
|---|---|---|
| lvalue | Expression that refers to an object with persistent identity | Variable name: x, arr[i], *ptr |
| rvalue | Temporary expression without persistent identity | Literal: 42, temporary: std::string{"hello"} |
| glvalue | Generalized lvalue (lvalue or xvalue) | Has identity, may or may not be movable |
| prvalue | Pure rvalue, temporary about to be created | Literals, return x + y; |
| xvalue | Expiring value, object about to expire | Result of std::move(x) |
Move Operations
| Term | Definition | Example |
|---|---|---|
| Move Constructor | Constructor that transfers resources from another object | MyClass(MyClass&& other) noexcept |
| Move Assignment Operator | Assignment operator that transfers resources | MyClass& operator=(MyClass&& other) noexcept |
| std::move | Casts lvalue to rvalue reference, enabling move | MyClass obj2{std::move(obj1)}; |
| std::forward | Perfect forwarding preserving value category | template<typename T> void f(T&& t) { g(std::forward<T>(t)); } |
| Move-enabled | Class supporting move operations | Class with move constructor and move assignment |
Reference Types
| Term | Definition | Example |
|---|---|---|
| lvalue Reference | Reference binding to lvalue | int& ref{x}; |
| const lvalue Reference | Const reference, can bind to both lvalue and rvalue | const int& ref{42}; |
| rvalue Reference | Reference binding to rvalue, enables move | int&& ref{42}; or MyClass&& ref{std::move(obj)}; |
| Forwarding Reference | Universal reference in template context | template<typename T> void f(T&& param) |
| Reference Collapsing | Rules for combining reference types | T& && becomes T& |
Resource Management
| Term | Definition | Example |
|---|---|---|
| Resource Transfer | Moving ownership from source to destination | Move constructor transfers pointer ownership |
| Moved-from State | Valid but unspecified state after move | Object is safe to destroy or reassign |
| Self-move | Moving object to itself, must be handled safely | Check this != &other in move assignment |
| noexcept Guarantee | Promise that operation won't throw exceptions | MyClass(MyClass&& other) noexcept |
| Resource Stealing | Taking resources from temporary or moved-from object | Moving pointer and setting source to nullptr |
Optimization Techniques
| Term | Definition | Example |
|---|---|---|
| Copy Elision | Compiler optimization eliminating copy/move | Return value optimization (RVO) |
| RVO | Return Value Optimization, eliminates return copy | return MyClass{}; |
| NRVO | Named Return Value Optimization | MyClass result; return result; |
| Move on Return | Automatic move when returning local variable | return obj; where obj is local |
| Temporary Materialization | Creating temporary object from prvalue | Binding prvalue to const reference |
Special Member Functions
| Term | Definition | Example |
|---|---|---|
| Rule of Zero | If possible, define no special members | Use smart pointers, let compiler generate |
| Rule of Three | Define copy constructor, copy assignment, destructor together | For classes managing resources |
| Rule of Five | Add move constructor and move assignment to rule of three | Modern C++ resource management |
| Defaulted Move | Compiler-generated move operations | MyClass(MyClass&&) = default; |
| Deleted Move | Explicitly prevent move operations | MyClass(MyClass&&) = delete; |
Performance Concepts
| Term | Definition | Example |
|---|---|---|
| Move vs Copy | Move transfers ownership, copy duplicates | Move is O(1), copy might be O(n) |
| Cheap to Move | Type where move is much faster than copy | std::vector, std::string, std::unique_ptr |
| Expensive to Move | Type where move isn't significantly cheaper | Small types like int, double |
| Move-only Type | Type that can be moved but not copied | std::unique_ptr, std::thread |
| Zero-overhead Move | Move operation with no runtime cost | Just pointer swap |
Template and Forwarding
| Term | Definition | Example |
|---|---|---|
| Perfect Forwarding | Preserving value category through function calls | std::forward<T>(arg) in template |
| Universal Reference | T&& in template, can bind to lvalue or rvalue |
template<typename T> void f(T&& param) |
| Reference Collapsing | Combining references resolves to single reference type | T& && → T&, T&& && → T&& |
| Template Argument Deduction | Compiler inferring template type from argument | Determines if T&& is lvalue or rvalue reference |
| std::move vs std::forward | move unconditionally casts, forward conditionally | Use move for rvalues, forward in templates |
Common Patterns
| Term | Definition | Example |
|---|---|---|
| Swap Idiom | Efficient swap using move operations | std::swap uses move for efficiency |
| Sink Function | Function taking parameter by value to move into | void setName(std::string name) { m_name = std::move(name); } |
| Move Capture | Capturing by move in lambda | [ptr = std::move(unique_ptr)]() { } |
| Return by Value | Return local object, enables move/copy elision | MyClass createObject() { return MyClass{}; } |
| Pass by Value for Sink | Take by value when storing parameter | Enables move when called with rvalue |
Safety and Correctness
| Term | Definition | Example |
|---|---|---|
| Valid but Unspecified | Moved-from object is destructible and assignable | Don't rely on specific moved-from state |
| Conditional noexcept | noexcept specification depends on template parameter | noexcept(std::is_nothrow_move_constructible_v<T>) |
| Strong Exception Guarantee | Operation succeeds or object unchanged | Hard with move, often use copy-and-swap |
| Basic Exception Guarantee | Object valid but may be modified if exception | Typical for move operations |
| No-throw Swap | Swap operation guaranteed not to throw | Required for strong exception guarantee |
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Move Semantics Terminology - Quiz
Test your understanding of the lesson.
7 questions
10 minutes
60% to pass
Lesson Discussion
Share your thoughts and questions
💬
No comments yet. Be the first to share your thoughts!