Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Forwarding references and std::forward

Reference for C++ forwarding references: how T&& deduces in templates, reference collapsing, std::forward for perfect forwarding, and auto&& in lambdas.

A forwarding reference (Scott Meyers called it a "universal reference") is a function parameter written T&& where T is a template parameter being deduced, or a variable declared auto&&. Despite the && it is not an rvalue reference: it binds to anything. If you pass an lvalue, T is deduced as an lvalue reference type; if you pass an rvalue, T is deduced as a non-reference type. Combined with std::forward<T>(arg), this lets a wrapper pass its arguments to another function with the same value category they arrived with. This is perfect forwarding, and it is how std::make_unique, emplace_back, std::thread and std::invoke work.

C++
template <class T>
void relay(T&& arg) {                 // forwarding reference
    target(std::forward<T>(arg));     // preserve lvalue-ness or rvalue-ness
}

Try it

C++ Perfect forwarding preserves value category

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++ A factory function and auto&& in a lambda

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> void f(T&& param);          // forwarding reference
template <class... Ts> void g(Ts&&... params); // pack of forwarding references
auto&& v = expr;                               // forwarding reference variable
std::forward<T>(param)                         // <utility>
std::forward_like<Owner>(member)               // <utility>, C++23
Name Description
T&& with T deduced Binds to lvalues (T = U&) and rvalues (T = U).
std::forward<T>(x) Returns x as T&&: an rvalue if T is a non-reference, an lvalue if T is an lvalue reference.
std::forward_like<Owner>(m) Returns m with the const-ness and value category that Owner would give it (C++23).
Reference collapsing T& &, T& && and T&& & become T&; only T&& && becomes T&&.

Errors / undefined behavior: forwarding the same argument twice (f(std::forward<T>(a)); g(std::forward<T>(a));) can use a moved-from object. Calling std::forward with a type that does not match the deduced one, for example forwarding an lvalue as an rvalue, is incorrect and can move from an object the caller still uses.

Usage notes

  • What is not a forwarding reference. void f(std::vector<T>&&) is an rvalue reference (the type is not exactly T&&); const T&& is an rvalue reference to const; a class template's member void push(T&&) is an rvalue reference once T is fixed by the class.
  • Use std::forward only on forwarding references, on the last use. Use std::move on genuine rvalue references.
  • Overloading on T&& is greedy. A forwarding-reference constructor template<class T> Widget(T&&) is a better match than the copy constructor for a non-const lvalue Widget. Constrain it with requires or enable_if.
  • Braced lists cannot be deduced: f({1, 2}) fails for T&&. Give a type explicitly or add an overload taking std::initializer_list.
  • Perfect forwarding is imperfect for braced initializers, 0/NULL as null pointers, overloaded function names and bit-fields.
  • Concepts make it clearer: void f(std::integral auto&& x) still forwards, and requires std::constructible_from<T, Args...> documents the contract.
  • decltype(arg) in generic lambdas gives the type to pass to std::forward, as in the call lambda above.
  • Since C++11 (auto&& C++11; generic lambdas C++14; forward_like C++23).