Module lessons (1/4)
Move Semantics, Rvalue (&&) and std::move
Lvalue vs rvalue, O(1) shift constructors/assignments, noexcept, and the 0/5 rule.
What you'll learn in this lesson
- Distinguish lvalue (objects with a persistent name and address) from rvalue (temporary ready to be consumed).
- Clear allocations and deep copies
O(N)by writing move constructor and move assignmentO(1)markednoexcept. - Apply the Rule of Zeros (preferring
std::unique_ptr/std::vector) or Fives, including the valid-but-unspecified state afterstd::move.
Overview
Before C++11, returning a large std::vector or placing it in a collection risked duplicating every byte on the Heap. Move Semantics allow you to move the buffer pointer in just 3 CPU register assignments (O(1)), leaving the source empty and ready for destruction.
An lvalue has an in-memory identity and a name (int x = 5;); an rvalue is an unnamed ephemeral temporary value that is about to be destroyed (e.g. 42, "ciao", makeBuffer()). An rvalue reference (Buffer&&) hooks exclusively to these temporaries or objects converted with std::move:
Sections covered in this lesson
- 11. Lvalue vs Rvalue and
T&& - 22. Interactive Simulator: Deep Copy O(N) vs
std::moveO(1) - 33. Implement Move Constructor & Move Assignment
noexcept - 44. Rule of Zero vs Rule of Five
- 5Practical exercises
- 6Check for understanding
Unlock "Move Semantics, Rvalue (&&) and std::move" and the full Course C++ curriculum
Lvalue vs rvalue, O(1) shift constructors/assignments, noexcept, and the 0/5 rule.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course C++ plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.