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.
template <class T> void relay(T&& arg) { // forwarding reference target(std::forward<T>(arg)); // preserve lvalue-ness or rvalue-ness }
Try it
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.
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
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 exactlyT&&);const T&&is an rvalue reference to const; a class template's membervoid push(T&&)is an rvalue reference onceTis fixed by the class. - Use
std::forwardonly on forwarding references, on the last use. Usestd::moveon genuine rvalue references. - Overloading on
T&&is greedy. A forwarding-reference constructortemplate<class T> Widget(T&&)is a better match than the copy constructor for a non-const lvalueWidget. Constrain it withrequiresorenable_if. - Braced lists cannot be deduced:
f({1, 2})fails forT&&. Give a type explicitly or add an overload takingstd::initializer_list. - Perfect forwarding is imperfect for braced initializers,
0/NULLas null pointers, overloaded function names and bit-fields. - Concepts make it clearer:
void f(std::integral auto&& x)still forwards, andrequires std::constructible_from<T, Args...>documents the contract. decltype(arg)in generic lambdas gives the type to pass tostd::forward, as in thecalllambda above.- Since C++11 (
auto&&C++11; generic lambdas C++14;forward_likeC++23).