Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::move and std::forward

Reference for std::move, std::move_if_noexcept and std::forward: what the casts really do, moved-from state, perfect forwarding and the common mistakes.

std::move and std::forward are declared in <utility>. Neither moves or forwards anything by itself: both are casts that change the value category of an expression. std::move(x) casts x to an rvalue reference (T&&), which tells overload resolution that x may be pilfered. The actual move happens when something, usually a move constructor or move assignment, accepts that rvalue.

std::forward<T>(x) is the conditional version, used inside templates with forwarding references. It casts to an rvalue only if x was originally passed as an rvalue, and keeps lvalues as lvalues. That is perfect forwarding.

C++
std::string b = std::move(a);          // a is now valid but unspecified
template<class T> void f(T&& x) { g(std::forward<T>(x)); }

Try it

C++ What std::move does to a string and a vector

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Perfect forwarding with std::forward

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class T> constexpr remove_reference_t<T>&& move(T&& t) noexcept;
template<class T> constexpr T&& forward(remove_reference_t<T>& t) noexcept;
template<class T> constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
template<class T> constexpr conditional_t<..., const T&, T&&> move_if_noexcept(T& x) noexcept;
template<class T> constexpr auto forward_like(auto&& x) noexcept;    // C++23
Name Description
move(t) Unconditional cast to T&&.
forward<T>(t) Cast to T&&; T must be given explicitly, and is deduced as U& for lvalues, U for rvalues.
move_if_noexcept(x) Casts to rvalue only if the move constructor cannot throw (or there is no copy). Used by vector to keep its strong guarantee.
forward_like<T>(x) C++23: forwards x with the const-ness and category of T.

Returns: a reference to the same object with a different value category. Errors: none at runtime. Moving from const objects silently falls back to copying.

Usage notes

  • Moved-from objects are valid but have an unspecified value. You may destroy them or assign to them. Calling functions without preconditions (size(), empty()) is allowed; reading the value is not meaningful.
  • Do not std::move a returned local: return std::move(local); blocks copy elision and is usually slower. return local; already moves if elision does not apply. GCC warns with -Wpessimizing-move.
  • Do move when passing a named object to a sink parameter, or when assigning a member from a by-value parameter: name_ = std::move(name);.
  • Only a forwarding reference (T&& where T is deduced, or auto&&) should be combined with std::forward. A T&& of a class template parameter is an ordinary rvalue reference.
  • Use each forwarded variable once. After std::forward<T>(x) may have moved from x, do not use x again.
  • move on a const object produces const T&&, which cannot bind to a move constructor; the copy constructor is chosen.
  • Primitive types: moving an int is a copy; the source keeps its value.
  • Add noexcept to your own move operations so containers prefer moving over copying during reallocation.
  • Algorithms: std::move(first, last, out) in <algorithm> is a different function that moves a range.