Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of std::forward in c++

Tags:

c++

std

c++11

I have come across a code, where std::forward is used. I have googled about it for a longtime and not able to understand its real purpose and use.

I have seen similar threads in stackoverflow, but still not clear. Can somebody explain it with a simple example?

PS: I have gone through this page, but still not able to appreciate its use. Please do not flag this question duplicate and rather try to help me out.

like image 358
SKPS Avatar asked Dec 13 '25 13:12

SKPS


1 Answers

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.

like image 70
Stefano Falasca Avatar answered Dec 15 '25 02:12

Stefano Falasca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!