How does perfect forwarding work? [duplicate] - c++

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues?
template <typename T1, typename T2>
void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}

You have to understand the forwarding problem. You can read the entire problem in detail, but I'll summarize.
Basically, given the expression E(a, b, ... , c), we want the expression f(a, b, ... , c) to be equivalent. In C++03, this is impossible. There are many attempts, but they all fail in some regard.
The simplest is to use an lvalue-reference:
template <typename A, typename B, typename C>
void f(A& a, B& b, C& c)
{
E(a, b, c);
}
But this fails to handle temporary values (rvalues): f(1, 2, 3);, as those cannot be bound to an lvalue-reference.
The next attempt might be:
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c)
{
E(a, b, c);
}
Which fixes the above problem because "const X& binds to everything", including both lvalues and rvalues, but this causes a new problem. It now fails to allow E to have non-const arguments:
int i = 1, j = 2, k = 3;
void E(int&, int&, int&);
f(i, j, k); // oops! E cannot modify these
The third attempt accepts const-references, but then const_cast's the const away:
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c)
{
E(const_cast<A&>(a), const_cast<B&>(b), const_cast<C&>(c));
}
This accepts all values, can pass on all values, but potentially leads to undefined behavior:
const int i = 1, j = 2, k = 3;
E(int&, int&, int&);
f(i, j, k); // ouch! E can modify a const object!
A final solution handles everything correctly...at the cost of being impossible to maintain. You provide overloads of f, with all combinations of const and non-const:
template <typename A, typename B, typename C>
void f(A& a, B& b, C& c);
template <typename A, typename B, typename C>
void f(const A& a, B& b, C& c);
template <typename A, typename B, typename C>
void f(A& a, const B& b, C& c);
template <typename A, typename B, typename C>
void f(A& a, B& b, const C& c);
template <typename A, typename B, typename C>
void f(const A& a, const B& b, C& c);
template <typename A, typename B, typename C>
void f(const A& a, B& b, const C& c);
template <typename A, typename B, typename C>
void f(A& a, const B& b, const C& c);
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c);
N arguments require 2N combinations, a nightmare. We'd like to do this automatically.
(This is effectively what we get the compiler to do for us in C++11.)
In C++11, we get a chance to fix this. One solution modifies template deduction rules on existing types, but this potentially breaks a great deal of code. So we have to find another way.
The solution is to instead use the newly added rvalue-references; we can introduce new rules when deducing rvalue-reference types and create any desired result. After all, we cannot possibly break code now.
If given a reference to a reference (note reference is an encompassing term meaning both T& and T&&), we use the following rule to figure out the resulting type:
"[given] a type TR that is a reference to a type T, an attempt to create the type “lvalue reference to cv TR” creates the type “lvalue reference to T”, while an attempt to create the type “rvalue reference to cv TR” creates the type TR."
Or in tabular form:
TR R
T& & -> T& // lvalue reference to cv TR -> lvalue reference to T
T& && -> T& // rvalue reference to cv TR -> TR (lvalue reference to T)
T&& & -> T& // lvalue reference to cv TR -> lvalue reference to T
T&& && -> T&& // rvalue reference to cv TR -> TR (rvalue reference to T)
Next, with template argument deduction: if an argument is an lvalue A, we supply the template argument with an lvalue reference to A. Otherwise, we deduce normally. This gives so-called universal references (the term forwarding reference is now the official one).
Why is this useful? Because combined we maintain the ability to keep track of the value category of a type: if it was an lvalue, we have an lvalue-reference parameter, otherwise we have an rvalue-reference parameter.
In code:
template <typename T>
void deduce(T&& x);
int i;
deduce(i); // deduce<int&>(int& &&) -> deduce<int&>(int&)
deduce(1); // deduce<int>(int&&)
The last thing is to "forward" the value category of the variable. Keep in mind, once inside the function the parameter could be passed as an lvalue to anything:
void foo(int&);
template <typename T>
void deduce(T&& x)
{
foo(x); // fine, foo can refer to x
}
deduce(1); // okay, foo operates on x which has a value of 1
That's no good. E needs to get the same kind of value-category that we got! The solution is this:
static_cast<T&&>(x);
What does this do? Consider we're inside the deduce function, and we've been passed an lvalue. This means T is a A&, and so the target type for the static cast is A& &&, or just A&. Since x is already an A&, we do nothing and are left with an lvalue reference.
When we've been passed an rvalue, T is A, so the target type for the static cast is A&&. The cast results in an rvalue expression, which can no longer be passed to an lvalue reference. We've maintained the value category of the parameter.
Putting these together gives us "perfect forwarding":
template <typename A>
void f(A&& a)
{
E(static_cast<A&&>(a));
}
When f receives an lvalue, E gets an lvalue. When f receives an rvalue, E gets an rvalue. Perfect.
And of course, we want to get rid of the ugly. static_cast<T&&> is cryptic and weird to remember; let's instead make a utility function called forward, which does the same thing:
std::forward<A>(a);
// is the same as
static_cast<A&&>(a);

I think to have a conceptual code implementing std::forward can help with the understanding. This is a slide from Scott Meyers talk An Effective C++11/14 Sampler
Function move in the code is std::move. There is a (working) implementation for it earlier in that talk. I found actual implementation of std::forward in libstdc++, in file move.h, but it is not at all instructive.
From a user's perspective, the meaning of it is that std::forward is a conditional cast to an rvalue. It can be useful if I am writing a function which expects either an lvalue or rvalue in a parameter and wants to pass it to another function as an rvalue only if it was passed in as an rvalue. If I did not wrap the parameter in std::forward, it would be always passed as a normal reference.
#include <iostream>
#include <string>
#include <utility>
void overloaded_function(std::string& param) {
std::cout << "std::string& version" << std::endl;
}
void overloaded_function(std::string&& param) {
std::cout << "std::string&& version" << std::endl;
}
template<typename T>
void pass_through(T&& param) {
overloaded_function(std::forward<T>(param));
}
int main() {
std::string pes;
pass_through(pes);
pass_through(std::move(pes));
}
Sure enough, it prints
std::string& version
std::string&& version
The code is based on an example from the previously mentioned talk. Slide 10, at about 15:00 from the start.

In perfect forwarding, std::forward is used to convert the named rvalue reference t1 and t2 to unnamed rvalue reference. What is the purpose of doing that? How would that effect the called function inner if we leave t1 & t2 as lvalue?
template <typename T1, typename T2> void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}
If you use a named rvalue reference in an expression it is actually an lvalue (because you refer to the object by name). Consider the following example:
void inner(int &, int &); // #1
void inner(int &&, int &&); // #2
Now, if we call outer like this
outer(17,29);
we would like 17 and 29 to be forwarded to #2 because 17 and 29 are integer literals and as such rvalues. But since t1 and t2 in the expression inner(t1,t2); are lvalues, you'd be invoking #1 instead of #2. That's why we need to turn the references back into unnamed references with std::forward. So, t1 in outer is always an lvalue expression while forward<T1>(t1) may be an rvalue expression depending on T1. The latter is only an lvalue expression if T1 is an lvalue reference. And T1 is only deduced to be an lvalue reference in case the first argument to outer was an lvalue expression.

How would that affect the called function inner if we leave t1 & t2 as lvalue?
If, after instantiating, T1 is of type char, and T2 is of a class, you want to pass t1 per copy and t2 per const reference. Well, unless inner() takes them per non-const reference, that is, in which case you want to do so, too.
Try to write a set of outer() functions which implement this without rvalue references, deducing the right way to pass the arguments from inner()'s type. I think you'll need something 2^2 of them, pretty hefty template-meta stuff to deduce the arguments, and a lot of time to get this right for all cases.
And then someone comes along with an inner() that takes arguments per pointer. I think that now makes 3^2. (Or 4^2. Hell, I can't be bothered to try to think whether const pointer would make a difference.)
And then imagine you want to do this for a five parameters. Or seven.
Now you know why some bright minds came up with "perfect forwarding": It makes the compiler do all this for you.

A point that hasn't been made crystal clear is that static_cast<T&&> handles const T& properly too.
Program:
#include <iostream>
using namespace std;
void g(const int&)
{
cout << "const int&\n";
}
void g(int&)
{
cout << "int&\n";
}
void g(int&&)
{
cout << "int&&\n";
}
template <typename T>
void f(T&& a)
{
g(static_cast<T&&>(a));
}
int main()
{
cout << "f(1)\n";
f(1);
int a = 2;
cout << "f(a)\n";
f(a);
const int b = 3;
cout << "f(const b)\n";
f(b);
cout << "f(a * b)\n";
f(a * b);
}
Produces:
f(1)
int&&
f(a)
int&
f(const b)
const int&
f(a * b)
int&&
Note that 'f' has to be a template function. If it's just defined as 'void f(int&& a)' this doesn't work.

It may be worthwhile to emphasize that forward has to be used in tandem with an outer method with forwarding/universal reference. Using forward by itself as the following statements is allowed, but does no good other than causing confusion. The standard committee may want to disable such flexibility otherwise why don't we just use static_cast instead?
std::forward<int>(1);
std::forward<std::string>("Hello");
In my opinion, move and forward are design patterns which are natural outcomes after r-value reference type is introduced. We should not name a method assuming it is correctly used unless incorrect usage is forbidden.

From another viewpoint, when dealing with rvalues in a universal reference assignment, it may be desirable to preserve the type of a variable as it is. For example
auto&& x = 2; // x is int&&
auto&& y = x; // But y is int&
auto&& z = std::forward<decltype(x)>(x); // z is int&&
Using std::forward, we ensured z exactly has the same type as x.
Moreover, std::forward doesn't affect lvalue references:
int i;
auto&& x = i; // x is int&
auto&& y = x; // y is int&
auto&& z = std::forward<decltype(x)>(x); // z is int&
Still z has the same type as x.
So, back to your case, if the inner function has two overloads for int& and int&&, you want to pass variables like z assignment not y one.
The types in the example can be assessed via:
std::cout<<is_same_v<int&,decltype(z)>;
std::cout<<is_same_v<int&&,decltype(z)>;

Related

function template overload with rvaule reference as argument does not work?

The function template with && as argument seems cannot be overloaded, when input is not rvalue. See here as example:
template<typename A, typename B>
void test_tp_func(A&& a, B&& b)
{
std::cout<<"tp1(" << a << "," << b << ")\n";
}
template<typename A>
void test_tp_func(A&& a, int&& b)
{
std::cout<<"tp2(" << a << "," << b << ")\n";
}
int main()
{
test_tp_func(1, 2);
int i{10};
const int& ir = i;
test_tp_func(2, ir);
test_tp_func(2, std::move(i));
}
The output is:
tp2(1,2)
tp1(2,10)
tp2(2,10)
We can see the test_tp_func(2, ir); is not using the overload at all. How can I make sure it use the test_tp_func(A&& a, int&& b)? One way is to add enable_if_t to disable the original template when B is int. However, the original template test_tp_func(A&& a, B&& b) is defined in a file that is not in my control.
updated the example to actually use const&
How can you choose the rvalue overload? It would help if the argument was an rvalue which i is not.
It's not a prvalue (it's a named variable) and it isn't an xvalue either.
If you just want to intercept the case where B = int, why not just make the argument type int? Using int&& will only match rvalues by definition.
As discussed in comments, you can't simply replace the forwarding reference B&& with an rvalue reference int&& (or any other concrete type in place of int) because forwarding references get special treatment during argument deduction (see here and search for "forwarding reference", it's a special case).
You need two overloads: the const ref const int&, and the rvalue ref int&&, to cover the same cases as the original forwarding reference.

How to construct an object either from a const reference or temporary via forwarding template

Consider this minimal example
template <class T>
class Foo
{
public:
Foo(const T& t_)
: t(t_)
{
}
Foo(T&& t_)
: t(std::move(t_))
{
}
T t;
};
template <typename F>
Foo<F> makeFoo(F&& f)
{
return Foo<F>(std::forward<F>(f));
}
int main()
{
class C
{
};
C c;
makeFoo(c);
}
MSVC 2017 fails with a redefinition error of Foo's ctor. Apparently T gets deduced to C& instead of the intended C. How exactly does that happen and how to modify the code so that it does what is inteded: either copy construct Foo::t from a const reference or move construct it from an r-value.
In C++17 you can simply write:
template <typename F>
auto makeFoo(F&& f)
{
return Foo(std::forward<F>(f));
}
because of class template argument deduction.
In C++14 you can write:
template <typename F>
auto makeFoo(F&& f)
{
return Foo<std::decay_t<F>>(std::forward<F>(f));
}
template <class F, class R = std::decay_t<F>>
Foo<R> makeFoo(F&& f)
{
return Foo<R>(std::forward<F>(f));
}
that is a clean and simple way to solve your problem.
Decay is an appropriate way to convert a type into a type suitable for storing somewhere. It does bad things with array types but otherwise does pretty much the right thing; your code doesn't work with array types anyhow.
The compiler error is due to reference collapsing rules.
X X& X const& X&&
int int& int const& int&&
int& int& int& int&
int const int const& int const& int const&&
int&& int& int& int&&
int const& int const& int const& int const&
these may seem strange.
The first rule is that a const reference is a reference, but a reference to const is different. You cannot qualify the "reference" part; you can only const-qualify the referred part.
When you have T=int&, when you calculate T const or const T, you just get int&.
The second part has to do with how using r and l value references together work. When you do int& && or int&& & (which you cannot do directly; instead you do T=int& then T&& or T=int&& and T&), you always get an lvalue reference -- T&. lvalue wins out over rvalue.
Then we add in the rules for how T&& types are deduced; if you pass a mutable lvalue of type C, you get T=C& in the call to makeFoo.
So you had:
template<F = C&>
Foo<C&> makeFoo( C& && f )
as your signature, aka
template<F = C&>
Foo<C&> makeFoo( C& f )
now we examine Foo<C&>. It has two ctors:
Foo( C& const& )
Foo( C& && )
for the first one, const on a reference is discarded:
Foo( C& & )
Foo( C& && )
next, a reference to a reference is a reference, and lvalue references win out over rvalue references:
Foo( C& )
Foo( C& )
and there we go, two identical signature constructors.
TL;DR -- do the thing at the start of this answer.
Issue is that typename provided to class is reference in one case:
template <typename F>
Foo<F> makeFoo(F&& f)
{
return Foo<F>(std::forward<F>(f));
}
becomes
template <>
Foo<C&> makeFoo(C& f)
{
return Foo<C&>(std::forward<C&>(f));
}
You probably want some decay:
template <typename F>
Foo<std::decay_t<F>> makeFoo(F&& f)
{
return Foo<std::decay_t<F>>(std::forward<F>(f));
}
This happens because of reference collapsing.
The F&& in your code is a forwarding reference, which means it can be either an lvalue reference or an rvalue reference depending on the type of the argument to which it binds.
In your case, if F&& binds to an argument of type C&& (an rvalue reference to C), F is simply deduced as C. However, if F&& binds to an argument of type C& (as in your example), the reference collapsing rules determine the type deduced for F:
T& & -> T&
T& && -> T&
T&& & -> T&
T&& && -> T&&
Thus, F is deduced as C&, since C& && collapses to C&.
You can use remove_reference to remove any reference from the deduced type:
remove_reference_t<C> -> C
remove_reference_t<C&> -> C
You will probably also want to use remove_cv to remove any potential const (or volatile) qualifier:
remove_cv_t<remove_reference_t<C>> -> C
remove_cv_t<remove_reference_t<C&>> -> C
remove_cv_t<remove_reference_t<C const>> -> C
remove_cv_t<remove_reference_t<C const&>> -> C
In C++20, there is a combined remove_cvref trait which can save some typing. However, many implementations just use decay, which does the same thing, but also turns array and function types into pointers, which may or may not be desirable depending on your use case (some parts of the standard library have switched from using decay to using remove_cvref in C++20).

What is the proper way to declare a function that can take either rvalue or lvalue reference?

Suppose I want to have a function foo taking either lvalue or rvalue reference as the parameter.
I could break it into two overloads taking lvalue and rvalue references.
void foo(int& a){/*some impl*/} // lvalue ref
void foo(int&& a){foo(a);} // rvalue ref
int main(){
int a;
foo(a); // lvalue
foo(1); // rvalue
}
It does work but is pretty verbose. I could improve it using templates.
template<typename T>
void foo(T &&a) { /*some impl*/ }
int main(){
int a;
foo(a); // lvalue, T = int&
foo(1); // rvalue, T = int
}
For a binary function the template would need to take two template parameters.
template<typename T1, typename T2>
void foo(T1 &&a, T2 &&b) { /*some impl*/ }
int main(){
int a;
foo(a, a); // (lvalue, lvalue), T1 = int&, T2 = int&
foo(1, 1); // (rvalue, rvalue), T1 = int, T2 = int
foo(1, a); // (rvalue, lvalue), T1 = int, T2 = int&
foo(a, 1); // (lvalue, rvalue), T1 = int&, T2 = int
}
Is it the way to go? Is there any better way?
I have hardly any experience using cpp but it seems suspicious that I need to do such tricks to simply say "do not make copies of passed parameters".
I am using gcc 5.4.0 with -std=c++11.
-- UPDATE 1 --
I came up with the question when I was using streams library range(T&& lower, T&& upper) method which takes both T&& parameters. I can pass both lvalue or rvalue parameters to the function but it makes me unable to pass for example 0 and some_var as parameters. Whatever is the author's reasons for the function taking T&& parameters I was wondering if there is a way to extend the declaration to take mixed lvalue/rvalue parameters without sacrificing whatever the author wanted to achieve.
-- UPDATE 2 --
If the parameter is read-only const & can be used (#RichardCritten).
When you want to modify/return the parameter without copying you can use either templates or #Yakk solution.
#Yakk solution seems better when you declare a function that takes multiple parameters.
For example, if a function takes two int l/r-value reference parameters and returns int reference using templates leads to a messy signature.
template<typename T1, typename T2>
int& foo(T1 &&a, T2 &&b) {
a += b;
return a;
}
While #Yakk solution gives a pretty elegant one.
int& foo(any_ref<int> a, any_ref<int> b) {
a += b;
return a;
}
template<class T>
struct any_ref{
T& t;
any_ref(T&in):t(in){}
any_ref(T&&in):any_ref(in){}
any_ref(any_ref const&)=default;
any_ref& operator=(any_ref const&)=default;
~any_ref()=default;
operator T&()const&{return t;}
operator T&&()&&{return std::move(t);}// maybe
T& get()const{return *this;}
};
Then:
void foo(any_ref<int>a, any_ref<int>b);
is not a template function. The body can do a int&a=a_arg; if it wants real references.
But really, just take int& if you want a "maybe-out" parameter. Callers who want to pass an rvalue as a lvalue can write:
templare<class T>
T& as_lvalue(T&&t){return t;}
and cast their rvalueness away.
If the parameter is a read only reference, use const&.

Do I need T&& and std::forward when writing a templated `<<` operator? [duplicate]

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues?
template <typename T1, typename T2>
void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}
You have to understand the forwarding problem. You can read the entire problem in detail, but I'll summarize.
Basically, given the expression E(a, b, ... , c), we want the expression f(a, b, ... , c) to be equivalent. In C++03, this is impossible. There are many attempts, but they all fail in some regard.
The simplest is to use an lvalue-reference:
template <typename A, typename B, typename C>
void f(A& a, B& b, C& c)
{
E(a, b, c);
}
But this fails to handle temporary values (rvalues): f(1, 2, 3);, as those cannot be bound to an lvalue-reference.
The next attempt might be:
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c)
{
E(a, b, c);
}
Which fixes the above problem because "const X& binds to everything", including both lvalues and rvalues, but this causes a new problem. It now fails to allow E to have non-const arguments:
int i = 1, j = 2, k = 3;
void E(int&, int&, int&);
f(i, j, k); // oops! E cannot modify these
The third attempt accepts const-references, but then const_cast's the const away:
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c)
{
E(const_cast<A&>(a), const_cast<B&>(b), const_cast<C&>(c));
}
This accepts all values, can pass on all values, but potentially leads to undefined behavior:
const int i = 1, j = 2, k = 3;
E(int&, int&, int&);
f(i, j, k); // ouch! E can modify a const object!
A final solution handles everything correctly...at the cost of being impossible to maintain. You provide overloads of f, with all combinations of const and non-const:
template <typename A, typename B, typename C>
void f(A& a, B& b, C& c);
template <typename A, typename B, typename C>
void f(const A& a, B& b, C& c);
template <typename A, typename B, typename C>
void f(A& a, const B& b, C& c);
template <typename A, typename B, typename C>
void f(A& a, B& b, const C& c);
template <typename A, typename B, typename C>
void f(const A& a, const B& b, C& c);
template <typename A, typename B, typename C>
void f(const A& a, B& b, const C& c);
template <typename A, typename B, typename C>
void f(A& a, const B& b, const C& c);
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c);
N arguments require 2N combinations, a nightmare. We'd like to do this automatically.
(This is effectively what we get the compiler to do for us in C++11.)
In C++11, we get a chance to fix this. One solution modifies template deduction rules on existing types, but this potentially breaks a great deal of code. So we have to find another way.
The solution is to instead use the newly added rvalue-references; we can introduce new rules when deducing rvalue-reference types and create any desired result. After all, we cannot possibly break code now.
If given a reference to a reference (note reference is an encompassing term meaning both T& and T&&), we use the following rule to figure out the resulting type:
"[given] a type TR that is a reference to a type T, an attempt to create the type “lvalue reference to cv TR” creates the type “lvalue reference to T”, while an attempt to create the type “rvalue reference to cv TR” creates the type TR."
Or in tabular form:
TR R
T& & -> T& // lvalue reference to cv TR -> lvalue reference to T
T& && -> T& // rvalue reference to cv TR -> TR (lvalue reference to T)
T&& & -> T& // lvalue reference to cv TR -> lvalue reference to T
T&& && -> T&& // rvalue reference to cv TR -> TR (rvalue reference to T)
Next, with template argument deduction: if an argument is an lvalue A, we supply the template argument with an lvalue reference to A. Otherwise, we deduce normally. This gives so-called universal references (the term forwarding reference is now the official one).
Why is this useful? Because combined we maintain the ability to keep track of the value category of a type: if it was an lvalue, we have an lvalue-reference parameter, otherwise we have an rvalue-reference parameter.
In code:
template <typename T>
void deduce(T&& x);
int i;
deduce(i); // deduce<int&>(int& &&) -> deduce<int&>(int&)
deduce(1); // deduce<int>(int&&)
The last thing is to "forward" the value category of the variable. Keep in mind, once inside the function the parameter could be passed as an lvalue to anything:
void foo(int&);
template <typename T>
void deduce(T&& x)
{
foo(x); // fine, foo can refer to x
}
deduce(1); // okay, foo operates on x which has a value of 1
That's no good. E needs to get the same kind of value-category that we got! The solution is this:
static_cast<T&&>(x);
What does this do? Consider we're inside the deduce function, and we've been passed an lvalue. This means T is a A&, and so the target type for the static cast is A& &&, or just A&. Since x is already an A&, we do nothing and are left with an lvalue reference.
When we've been passed an rvalue, T is A, so the target type for the static cast is A&&. The cast results in an rvalue expression, which can no longer be passed to an lvalue reference. We've maintained the value category of the parameter.
Putting these together gives us "perfect forwarding":
template <typename A>
void f(A&& a)
{
E(static_cast<A&&>(a));
}
When f receives an lvalue, E gets an lvalue. When f receives an rvalue, E gets an rvalue. Perfect.
And of course, we want to get rid of the ugly. static_cast<T&&> is cryptic and weird to remember; let's instead make a utility function called forward, which does the same thing:
std::forward<A>(a);
// is the same as
static_cast<A&&>(a);
I think to have a conceptual code implementing std::forward can help with the understanding. This is a slide from Scott Meyers talk An Effective C++11/14 Sampler
Function move in the code is std::move. There is a (working) implementation for it earlier in that talk. I found actual implementation of std::forward in libstdc++, in file move.h, but it is not at all instructive.
From a user's perspective, the meaning of it is that std::forward is a conditional cast to an rvalue. It can be useful if I am writing a function which expects either an lvalue or rvalue in a parameter and wants to pass it to another function as an rvalue only if it was passed in as an rvalue. If I did not wrap the parameter in std::forward, it would be always passed as a normal reference.
#include <iostream>
#include <string>
#include <utility>
void overloaded_function(std::string& param) {
std::cout << "std::string& version" << std::endl;
}
void overloaded_function(std::string&& param) {
std::cout << "std::string&& version" << std::endl;
}
template<typename T>
void pass_through(T&& param) {
overloaded_function(std::forward<T>(param));
}
int main() {
std::string pes;
pass_through(pes);
pass_through(std::move(pes));
}
Sure enough, it prints
std::string& version
std::string&& version
The code is based on an example from the previously mentioned talk. Slide 10, at about 15:00 from the start.
In perfect forwarding, std::forward is used to convert the named rvalue reference t1 and t2 to unnamed rvalue reference. What is the purpose of doing that? How would that effect the called function inner if we leave t1 & t2 as lvalue?
template <typename T1, typename T2> void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}
If you use a named rvalue reference in an expression it is actually an lvalue (because you refer to the object by name). Consider the following example:
void inner(int &, int &); // #1
void inner(int &&, int &&); // #2
Now, if we call outer like this
outer(17,29);
we would like 17 and 29 to be forwarded to #2 because 17 and 29 are integer literals and as such rvalues. But since t1 and t2 in the expression inner(t1,t2); are lvalues, you'd be invoking #1 instead of #2. That's why we need to turn the references back into unnamed references with std::forward. So, t1 in outer is always an lvalue expression while forward<T1>(t1) may be an rvalue expression depending on T1. The latter is only an lvalue expression if T1 is an lvalue reference. And T1 is only deduced to be an lvalue reference in case the first argument to outer was an lvalue expression.
How would that affect the called function inner if we leave t1 & t2 as lvalue?
If, after instantiating, T1 is of type char, and T2 is of a class, you want to pass t1 per copy and t2 per const reference. Well, unless inner() takes them per non-const reference, that is, in which case you want to do so, too.
Try to write a set of outer() functions which implement this without rvalue references, deducing the right way to pass the arguments from inner()'s type. I think you'll need something 2^2 of them, pretty hefty template-meta stuff to deduce the arguments, and a lot of time to get this right for all cases.
And then someone comes along with an inner() that takes arguments per pointer. I think that now makes 3^2. (Or 4^2. Hell, I can't be bothered to try to think whether const pointer would make a difference.)
And then imagine you want to do this for a five parameters. Or seven.
Now you know why some bright minds came up with "perfect forwarding": It makes the compiler do all this for you.
A point that hasn't been made crystal clear is that static_cast<T&&> handles const T& properly too.
Program:
#include <iostream>
using namespace std;
void g(const int&)
{
cout << "const int&\n";
}
void g(int&)
{
cout << "int&\n";
}
void g(int&&)
{
cout << "int&&\n";
}
template <typename T>
void f(T&& a)
{
g(static_cast<T&&>(a));
}
int main()
{
cout << "f(1)\n";
f(1);
int a = 2;
cout << "f(a)\n";
f(a);
const int b = 3;
cout << "f(const b)\n";
f(b);
cout << "f(a * b)\n";
f(a * b);
}
Produces:
f(1)
int&&
f(a)
int&
f(const b)
const int&
f(a * b)
int&&
Note that 'f' has to be a template function. If it's just defined as 'void f(int&& a)' this doesn't work.
It may be worthwhile to emphasize that forward has to be used in tandem with an outer method with forwarding/universal reference. Using forward by itself as the following statements is allowed, but does no good other than causing confusion. The standard committee may want to disable such flexibility otherwise why don't we just use static_cast instead?
std::forward<int>(1);
std::forward<std::string>("Hello");
In my opinion, move and forward are design patterns which are natural outcomes after r-value reference type is introduced. We should not name a method assuming it is correctly used unless incorrect usage is forbidden.
From another viewpoint, when dealing with rvalues in a universal reference assignment, it may be desirable to preserve the type of a variable as it is. For example
auto&& x = 2; // x is int&&
auto&& y = x; // But y is int&
auto&& z = std::forward<decltype(x)>(x); // z is int&&
Using std::forward, we ensured z exactly has the same type as x.
Moreover, std::forward doesn't affect lvalue references:
int i;
auto&& x = i; // x is int&
auto&& y = x; // y is int&
auto&& z = std::forward<decltype(x)>(x); // z is int&
Still z has the same type as x.
So, back to your case, if the inner function has two overloads for int& and int&&, you want to pass variables like z assignment not y one.
The types in the example can be assessed via:
std::cout<<is_same_v<int&,decltype(z)>;
std::cout<<is_same_v<int&&,decltype(z)>;

What are the main purposes of std::forward and which problems does it solve?

In perfect forwarding, std::forward is used to convert the named rvalue references t1 and t2 to unnamed rvalue references. What is the purpose of doing that? How would that affect the called function inner if we leave t1 & t2 as lvalues?
template <typename T1, typename T2>
void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}
You have to understand the forwarding problem. You can read the entire problem in detail, but I'll summarize.
Basically, given the expression E(a, b, ... , c), we want the expression f(a, b, ... , c) to be equivalent. In C++03, this is impossible. There are many attempts, but they all fail in some regard.
The simplest is to use an lvalue-reference:
template <typename A, typename B, typename C>
void f(A& a, B& b, C& c)
{
E(a, b, c);
}
But this fails to handle temporary values (rvalues): f(1, 2, 3);, as those cannot be bound to an lvalue-reference.
The next attempt might be:
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c)
{
E(a, b, c);
}
Which fixes the above problem because "const X& binds to everything", including both lvalues and rvalues, but this causes a new problem. It now fails to allow E to have non-const arguments:
int i = 1, j = 2, k = 3;
void E(int&, int&, int&);
f(i, j, k); // oops! E cannot modify these
The third attempt accepts const-references, but then const_cast's the const away:
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c)
{
E(const_cast<A&>(a), const_cast<B&>(b), const_cast<C&>(c));
}
This accepts all values, can pass on all values, but potentially leads to undefined behavior:
const int i = 1, j = 2, k = 3;
E(int&, int&, int&);
f(i, j, k); // ouch! E can modify a const object!
A final solution handles everything correctly...at the cost of being impossible to maintain. You provide overloads of f, with all combinations of const and non-const:
template <typename A, typename B, typename C>
void f(A& a, B& b, C& c);
template <typename A, typename B, typename C>
void f(const A& a, B& b, C& c);
template <typename A, typename B, typename C>
void f(A& a, const B& b, C& c);
template <typename A, typename B, typename C>
void f(A& a, B& b, const C& c);
template <typename A, typename B, typename C>
void f(const A& a, const B& b, C& c);
template <typename A, typename B, typename C>
void f(const A& a, B& b, const C& c);
template <typename A, typename B, typename C>
void f(A& a, const B& b, const C& c);
template <typename A, typename B, typename C>
void f(const A& a, const B& b, const C& c);
N arguments require 2N combinations, a nightmare. We'd like to do this automatically.
(This is effectively what we get the compiler to do for us in C++11.)
In C++11, we get a chance to fix this. One solution modifies template deduction rules on existing types, but this potentially breaks a great deal of code. So we have to find another way.
The solution is to instead use the newly added rvalue-references; we can introduce new rules when deducing rvalue-reference types and create any desired result. After all, we cannot possibly break code now.
If given a reference to a reference (note reference is an encompassing term meaning both T& and T&&), we use the following rule to figure out the resulting type:
"[given] a type TR that is a reference to a type T, an attempt to create the type “lvalue reference to cv TR” creates the type “lvalue reference to T”, while an attempt to create the type “rvalue reference to cv TR” creates the type TR."
Or in tabular form:
TR R
T& & -> T& // lvalue reference to cv TR -> lvalue reference to T
T& && -> T& // rvalue reference to cv TR -> TR (lvalue reference to T)
T&& & -> T& // lvalue reference to cv TR -> lvalue reference to T
T&& && -> T&& // rvalue reference to cv TR -> TR (rvalue reference to T)
Next, with template argument deduction: if an argument is an lvalue A, we supply the template argument with an lvalue reference to A. Otherwise, we deduce normally. This gives so-called universal references (the term forwarding reference is now the official one).
Why is this useful? Because combined we maintain the ability to keep track of the value category of a type: if it was an lvalue, we have an lvalue-reference parameter, otherwise we have an rvalue-reference parameter.
In code:
template <typename T>
void deduce(T&& x);
int i;
deduce(i); // deduce<int&>(int& &&) -> deduce<int&>(int&)
deduce(1); // deduce<int>(int&&)
The last thing is to "forward" the value category of the variable. Keep in mind, once inside the function the parameter could be passed as an lvalue to anything:
void foo(int&);
template <typename T>
void deduce(T&& x)
{
foo(x); // fine, foo can refer to x
}
deduce(1); // okay, foo operates on x which has a value of 1
That's no good. E needs to get the same kind of value-category that we got! The solution is this:
static_cast<T&&>(x);
What does this do? Consider we're inside the deduce function, and we've been passed an lvalue. This means T is a A&, and so the target type for the static cast is A& &&, or just A&. Since x is already an A&, we do nothing and are left with an lvalue reference.
When we've been passed an rvalue, T is A, so the target type for the static cast is A&&. The cast results in an rvalue expression, which can no longer be passed to an lvalue reference. We've maintained the value category of the parameter.
Putting these together gives us "perfect forwarding":
template <typename A>
void f(A&& a)
{
E(static_cast<A&&>(a));
}
When f receives an lvalue, E gets an lvalue. When f receives an rvalue, E gets an rvalue. Perfect.
And of course, we want to get rid of the ugly. static_cast<T&&> is cryptic and weird to remember; let's instead make a utility function called forward, which does the same thing:
std::forward<A>(a);
// is the same as
static_cast<A&&>(a);
I think to have a conceptual code implementing std::forward can help with the understanding. This is a slide from Scott Meyers talk An Effective C++11/14 Sampler
Function move in the code is std::move. There is a (working) implementation for it earlier in that talk. I found actual implementation of std::forward in libstdc++, in file move.h, but it is not at all instructive.
From a user's perspective, the meaning of it is that std::forward is a conditional cast to an rvalue. It can be useful if I am writing a function which expects either an lvalue or rvalue in a parameter and wants to pass it to another function as an rvalue only if it was passed in as an rvalue. If I did not wrap the parameter in std::forward, it would be always passed as a normal reference.
#include <iostream>
#include <string>
#include <utility>
void overloaded_function(std::string& param) {
std::cout << "std::string& version" << std::endl;
}
void overloaded_function(std::string&& param) {
std::cout << "std::string&& version" << std::endl;
}
template<typename T>
void pass_through(T&& param) {
overloaded_function(std::forward<T>(param));
}
int main() {
std::string pes;
pass_through(pes);
pass_through(std::move(pes));
}
Sure enough, it prints
std::string& version
std::string&& version
The code is based on an example from the previously mentioned talk. Slide 10, at about 15:00 from the start.
In perfect forwarding, std::forward is used to convert the named rvalue reference t1 and t2 to unnamed rvalue reference. What is the purpose of doing that? How would that effect the called function inner if we leave t1 & t2 as lvalue?
template <typename T1, typename T2> void outer(T1&& t1, T2&& t2)
{
inner(std::forward<T1>(t1), std::forward<T2>(t2));
}
If you use a named rvalue reference in an expression it is actually an lvalue (because you refer to the object by name). Consider the following example:
void inner(int &, int &); // #1
void inner(int &&, int &&); // #2
Now, if we call outer like this
outer(17,29);
we would like 17 and 29 to be forwarded to #2 because 17 and 29 are integer literals and as such rvalues. But since t1 and t2 in the expression inner(t1,t2); are lvalues, you'd be invoking #1 instead of #2. That's why we need to turn the references back into unnamed references with std::forward. So, t1 in outer is always an lvalue expression while forward<T1>(t1) may be an rvalue expression depending on T1. The latter is only an lvalue expression if T1 is an lvalue reference. And T1 is only deduced to be an lvalue reference in case the first argument to outer was an lvalue expression.
How would that affect the called function inner if we leave t1 & t2 as lvalue?
If, after instantiating, T1 is of type char, and T2 is of a class, you want to pass t1 per copy and t2 per const reference. Well, unless inner() takes them per non-const reference, that is, in which case you want to do so, too.
Try to write a set of outer() functions which implement this without rvalue references, deducing the right way to pass the arguments from inner()'s type. I think you'll need something 2^2 of them, pretty hefty template-meta stuff to deduce the arguments, and a lot of time to get this right for all cases.
And then someone comes along with an inner() that takes arguments per pointer. I think that now makes 3^2. (Or 4^2. Hell, I can't be bothered to try to think whether const pointer would make a difference.)
And then imagine you want to do this for a five parameters. Or seven.
Now you know why some bright minds came up with "perfect forwarding": It makes the compiler do all this for you.
A point that hasn't been made crystal clear is that static_cast<T&&> handles const T& properly too.
Program:
#include <iostream>
using namespace std;
void g(const int&)
{
cout << "const int&\n";
}
void g(int&)
{
cout << "int&\n";
}
void g(int&&)
{
cout << "int&&\n";
}
template <typename T>
void f(T&& a)
{
g(static_cast<T&&>(a));
}
int main()
{
cout << "f(1)\n";
f(1);
int a = 2;
cout << "f(a)\n";
f(a);
const int b = 3;
cout << "f(const b)\n";
f(b);
cout << "f(a * b)\n";
f(a * b);
}
Produces:
f(1)
int&&
f(a)
int&
f(const b)
const int&
f(a * b)
int&&
Note that 'f' has to be a template function. If it's just defined as 'void f(int&& a)' this doesn't work.
It may be worthwhile to emphasize that forward has to be used in tandem with an outer method with forwarding/universal reference. Using forward by itself as the following statements is allowed, but does no good other than causing confusion. The standard committee may want to disable such flexibility otherwise why don't we just use static_cast instead?
std::forward<int>(1);
std::forward<std::string>("Hello");
In my opinion, move and forward are design patterns which are natural outcomes after r-value reference type is introduced. We should not name a method assuming it is correctly used unless incorrect usage is forbidden.
From another viewpoint, when dealing with rvalues in a universal reference assignment, it may be desirable to preserve the type of a variable as it is. For example
auto&& x = 2; // x is int&&
auto&& y = x; // But y is int&
auto&& z = std::forward<decltype(x)>(x); // z is int&&
Using std::forward, we ensured z exactly has the same type as x.
Moreover, std::forward doesn't affect lvalue references:
int i;
auto&& x = i; // x is int&
auto&& y = x; // y is int&
auto&& z = std::forward<decltype(x)>(x); // z is int&
Still z has the same type as x.
So, back to your case, if the inner function has two overloads for int& and int&&, you want to pass variables like z assignment not y one.
The types in the example can be assessed via:
std::cout<<is_same_v<int&,decltype(z)>;
std::cout<<is_same_v<int&&,decltype(z)>;