0Pricing
C++ Academy · Lesson

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 state

All lessons in this course

  1. Lvalue vs Rvalue The Distinction
  2. Rvalue References and the && Syntax
  3. std::move and std::forward
  4. Move Constructors and Move Assignment
← Back to C++ Academy