std::move and std::forward
Cast lvalues to rvalues with std::move and preserve value category with std::forward.
Two Cousins, Different Jobs
std::move and std::forward are both compile-time casts. They look similar but solve different problems.
std::move: Always Cast to Rvalue
std::move(x) unconditionally casts x to an rvalue reference. The receiving function can then steal its resources.
std::string s = "Hello";
std::string t = std::move(s); // move-constructs t from s
// s is now in a valid-but-unspecified stateAll lessons in this course
- Lvalue vs Rvalue The Distinction
- Rvalue References and the && Syntax
- std::move and std::forward
- Move Constructors and Move Assignment