Use of std::forward in c++

As the page you linked poses it:

This is a helper function to allow perfect forwarding of arguments taken as rvalue references to deduced types, preserving any potential move semantics involved.

When you have a named value, as in

void f1(int& namedValue){
    ...
}

or in

void f2(int&& namedValue){
    ...
}

it evaluates, no matter what, to an lvalue.

One more step. Suppose you have a template function

template <typename T>
void f(T&& namedValue){
    ...
}

such function can either be called with an lvalue or with an rvalue; however, no matter what, namedValue evaluates to an lvalue.

Now suppose you have two overloads of an helper function

void helper(int& i){
    ...
}
void helper(int&& i){
    ...
}

calling helper from inside f

template <typename T>
void f(T&& namedValue){
    helper(namedValue);
}

will invariably call the first overload for helper, since namedValue is, well, a named value which, naturally, evaluates to an lvalue.

In order to get the second version called when appropriate (i.e. when f has been invoked with a rvalue parameter), you write

template <typename T>
void f(T&& namedValue){
    helper( std::forward<T>(namedValue) );
}

All of this is expressed much concisely in the documentation by the following

The need for this function stems from the fact that all named values (such as function parameters) always evaluate as lvalues (even those declared as rvalue references), and this poses difficulties in preserving potential move semantics on template functions that forward arguments to other functions.


Each expression is in exactly one of the following two value categories: lvalue or rvalue.

Normally if you call a function like:

template<typename T>
void f(T t);

template<typename T>
void g(T t)
{
    f(t);
}

The value category of the argument to g is lost between the call to g and f, because named parameters, like local variables, are always lvalues.

By using std::forward and adjusting the parameter to a "universal reference" that uses reference collapsing you can preserve the value category:

template<typename T>
void f(T&& t);

template<typename T>
void g(T&& t)
{
    f(forward<T>(t));
}

That's why it's called "forward", because you are "forwarding" the value category on, rather than losing it.

So in the example if you call g with an rvalue, then f will be called with an rvalue - rather than an lvalue.

Tags:

C++

C++11

Std