简体   繁体   中英

Use of std::forward in c++

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.

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 (ie 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.

It is used to preserve the exact type of an argument in templates when passing it to another function. For example:

template<class T>
void wrapper(T&& arg)
{
    foo(std::forward<T>(arg)); // Forward a single argument.
}

This works as follows:

  • If the function wrapper gets a std::string or const std::string& , then foo is called as if arg has type of const std::string& .

  • If the function wrapper gets a std::string& , then foo is called as if arg has type of std::string& .

  • If the function wrapper gets a std::string&& , then foo is called as if arg has type of std::string&& .

The problem that std::forward solves is that by the usual rules the type of arg within function is std::string even if we pass std::string&& to wrapper . std::forward allows to inject the actual type of T , be it T , T& , const T& or T&& , to the call site.

It's basic use is you're in function g that has been called like this:

g(T1 p1, T2 p2, /* ... */);

and you want to call function f with exactly the same types:

f(T1 p1, T2 p2, /* ... */);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM