Template allows lvalue to become bound with rvalue reference [duplicate] - c++

This question already has an answer here:
Why does a templated rvalue reference accept lvalues?
(1 answer)
Closed 4 years ago.
This program:
using namespace std;
#include <iostream>
#include <memory>
struct Dog
{
int legs;
} adog;
Dog gimmeadog() { return adog; }
void walk(Dog && d) { cout << "Nonconst right dog walk\n"; }
//template<class T> void walk(T && d) { d.legs=3; cout << "Nonconst right something walk\n"; }
int main() {
Dog mydog = gimmeadog();
walk(mydog);
return 0;
}
correctly fails to compile on gcc because:
error: cannot bind rvalue reference of type ‘Dog&&’ to lvalue of type ‘Dog’
walk(mydog);
but if you un-comment the template it happily binds and prints "Nonconst right something walk".
Why? What type is T taking when it works? Is this not defeating the object of rvalue references?

When T is a template argument T&& is a forwarding reference, not a r-value one. T gets deduced as T for r-values and T& for l-values. Such a reference binds to anything.

template<class T>
void walk(T && d)
in this case T&& is forwarding reference, when you pass Lvalue (mydog object) T is deduced to be T&, so signature of walk looks like
void walk(Dog& );
then you can bind mydog to Lvalue reference.

Related

Why does the rvalue parameter change to lvalue when used? [duplicate]

This question already has answers here:
Why is function parameter always an lvalue?
(1 answer)
C++11: Why rvalue reference parameter implicitly converted to lvalue
(2 answers)
r-value parameters in a function
(3 answers)
Is the r-value parameter really an l-value parameter inside the function scope?
(1 answer)
Closed 4 months ago.
I pass rvalue std::move(x) to testForward(T&& v), but it calls print(T& t) inside.
It seems that the rvalue v has changed to an lvalue before it calls print(). I do not know why this happened. Can anyone explain it?
#include<iostream>
using namespace std;
template<typename T>
void print(T& t) {
std::cout << "Lvalue ref" << std::endl;
}
template<typename T>
void print(T&& t) {
std::cout << "Rvalue ref" << std::endl;
}
template<typename T>
void testForward(T&& v) {
print(v); // call print(T& t);
}
int main(int argc, char* argv[])
{
int x = 1;
testForward(std::move(x)); // output: Lvalue ref
}
The value category of the expression v is an lvalue, because:
... Even if the variable's type is rvalue reference, the expression consisting of its name is an lvalue expression
If you want to forward a forwarding reference as its original category, use std::forward, ie,
template<typename T>
void testForward(T&& v) {
print(std::forward<T>(v));
}

Why this code prints out RValue instead of LValue

I was testing code out and got stuck on this.
Here's my code:
#include <iostream>
template<typename T>
void check(T&& other) {
std::cout << "Rvalue" << std::endl;
}
template<typename T>
void check(T& other) {
std::cout << "Lvalue" << std::endl;
}
template<typename T>
void call(T other) {
check(std::forward<T>(other));
}
int main() {
std::string t = "Cool";
call(t);
}
Output:
RValue
Why is the output of this "RValue"? I did pass a LValue and when it forwarded, didn't it forward as a LValue? Why did it call the RValue function of check?
To use std::forward properly, your argument type should be T &&, not T. Fix it like this:
template<typename T>
void call(T&& other) {
check(std::forward<T>(other));
}
Then we get the expected result.
Online demo
Reference for std::forward
When t is a forwarding reference (a function argument that is declared
as an rvalue reference to a cv-unqualified function template
parameter), this overload forwards the argument to another function
with the value category it had when passed to the calling function.

Why doesn't my forward_ function work for rvalues?

I've understood how std::move works and implemented my own version for practice only. Now I'm trying to understand how std::forward works:
I've implemented this so far:
#include <iostream>
template <typename T>
T&& forward_(T&& x)
{
return static_cast<T&&>(x);
}
/*template <typename T>
T&& forward_(T& x)
{
return static_cast<T&&>(x);
}*/
void incr(int& i)
{
++i;
}
void incr2(int x)
{
++x;
}
void incr3(int&& x)
{
++x;
}
template <typename T, typename F>
void call(T&& a, F func)
{
func(forward_<T>(a));
}
int main()
{
int i = 10;
std::cout << i << '\n';
call(i, incr);
std::cout << i << '\n';
call(i, incr2);
std::cout << i << '\n';
call(0, incr3); // Error: cannot bind rvalue reference of type int&& to lvalue of type int.
std::cout << "\ndone!\n";
}
Why must I provide the overloaded forward(T&) version taking an lvalue reference? As I understand it a forwarding reference can yield an lvalue or an rvalue depending on the type of its argument. So passing the prvalue literal 0 to call along with the incr3 function that takes an rvalue reference of type int&& normally doesn't need forward<T>(T&)?!
If I un-comment the forward_(T&) version it works fine!?
I'm still confused about: why if I only use the forward_(T&) version does it work for any value category? Then what is the point in having the one taking a forwarding reference forward_(T&&)?
If I un-comment the version taking lvalue reference to T& and the one taking forwarding reference T&& then the code works fine and I've added some messages inside both to check which one called. the result is the the one with T&& never called!
template <typename T>
T&& forward_(T& x)
{
std::cout << "forward_(T&)\n";
return static_cast<T&&>(x);
}
template <typename T>
T&& forward_(T&& x)
{
std::cout << "forward_(T&&)\n";
return static_cast<T&&>(x);
}
I mean running the same code in the driver program I've shown above.
A T&& reference stops being a forwarding reference if you manually specify T (instead of letting the compiler deduce it). If the T is not an lvalue reference, then T&& is an rvalue reference and won't accept lvalues.
For example, if you do forward_<int>(...), then the parameter is an rvalue reference and ... can only be an rvalue.
But if you do forward_(...), then the parameter is a forwarding reference and ... can have any value category. (Calling it like this makes no sense though, since forward_(x) will have the same value category as x itself.)
It is clear that you wander why having two versions of std::forward; one takes an l-value reference to the type parameter T& and the other takes a universal reference (forwarding) to the type parameter. T&&.
In your case you are using forward_ from inside the function template call which has forwarding reference too. The problem is that even that function call called with an rvalue it always uses forward_ for an lvalue because there's no way that call can pass its arguments without an object (parameter). Remember that a name of an object is an lvlaue even if it's initialized from an r-value. That is why always in your example forward_(T&) is called.
Now you ask why there's second version taking forwarding reference?
It is so simple and as you may have already guessed: it is used for r-values (the values not the names of those objects).
Here is an example:
template <typename T>
T&& forward_(T& x)
{
std::cout << "forward_(T&)\n";
return static_cast<T&&>(x);
}
template <typename T>
T&& forward_(T&& x)
{
std::cout << "forward_(T&&)\n";
return static_cast<T&&>(x);
}
int main()
{
int i = 10;
forward_(i); // forward(T&) (1)
forward_(5); // forward(T&&) (2)
forward_("Hi"); // forward(T&) (3)
}

Why rvalue reference pass as lvalue reference?

pass() reference argument and pass it to reference, however a rvalue argument actually called the reference(int&) instead of reference(int &&), here is my code snippet:
#include <iostream>
#include <utility>
void reference(int& v) {
std::cout << "lvalue" << std::endl;
}
void reference(int&& v) {
std::cout << "rvalue" << std::endl;
}
template <typename T>
void pass(T&& v) {
reference(v);
}
int main() {
std::cout << "rvalue pass:";
pass(1);
std::cout << "lvalue pass:";
int p = 1;
pass(p);
return 0;
}
the output is:
rvalue pass:lvalue
lvalue pass:lvalue
For p it is easy to understand according to reference collapsing rule, but why the template function pass v to reference() as lvalue?
template <typename T>
void pass(T&& v) {
reference(v);
}
You are using a Forwarding reference here quite alright, but the fact that there is now a name v, it's considered an lvalue to an rvalue reference.
Simply put, anything that has a name is an lvalue. This is why Perfect Forwarding is needed, to get full semantics, use std::forward
template <typename T>
void pass(T&& v) {
reference(std::forward<T>(v));
}
What std::forward<T> does is simply to do something like this
template <typename T>
void pass(T&& v) {
reference(static_cast<T&&>(v));
}
See this;
Why the template function pass v to reference() as lvalue?
That's because v is an lvalue. Wait, what? v is an rvalue reference. The important thing is that it is a reference, and thus an lvalue. It doesn't matter that it only binds to rvalues.
If you want to keep the value category, you will have to do perfect forwarding. Perfect forwarding means that if you pass an rvalue (like in your case), the function will be called with an rvalue (instead of an lvalue):
template <typename T>
void pass(T&& v) {
reference(std::forward<T>(v)); //forward 'v' to 'reference'
}

When do template parameters resolve to rvalue references to lvalue references, or the other way around?

I was reading about rvalue references and perfect forwarding when I came across this article on MSDN: https://msdn.microsoft.com/en-us/library/dd293668.aspx
My question is about this example from the article:
#include <iostream>
#include <string>
using namespace std;
template<typename T> struct S;
// The following structures specialize S by
// lvalue reference (T&), const lvalue reference (const T&),
// rvalue reference (T&&), and const rvalue reference (const T&&).
// Each structure provides a print method that prints the type of
// the structure and its parameter.
template<typename T> struct S<T&> {
static void print(T& t)
{
cout << "print<T&>: " << t << endl;
}
};
template<typename T> struct S<const T&> {
static void print(const T& t)
{
cout << "print<const T&>: " << t << endl;
}
};
template<typename T> struct S<T&&> {
static void print(T&& t)
{
cout << "print<T&&>: " << t << endl;
}
};
template<typename T> struct S<const T&&> {
static void print(const T&& t)
{
cout << "print<const T&&>: " << t << endl;
}
};
// This function forwards its parameter to a specialized
// version of the S type.
template <typename T> void print_type_and_value(T&& t)
{
S<T&&>::print(std::forward<T>(t));
}
// This function returns the constant string "fourth".
const string fourth() { return string("fourth"); }
int main()
{
// The following call resolves to:
// print_type_and_value<string&>(string& && t)
// Which collapses to:
// print_type_and_value<string&>(string& t)
string s1("first");
print_type_and_value(s1);
// The following call resolves to:
// print_type_and_value<const string&>(const string& && t)
// Which collapses to:
// print_type_and_value<const string&>(const string& t)
const string s2("second");
print_type_and_value(s2);
// The following call resolves to:
// print_type_and_value<string&&>(string&& t)
print_type_and_value(string("third"));
// The following call resolves to:
// print_type_and_value<const string&&>(const string&& t)
print_type_and_value(fourth());
}
My question is, why does this call:
print_type_and_value(s1);
resolve to:
print_type_and_value<string&>(string& &&t)
If my understanding is correct, string& && is an rvalue reference to an lvalue reference. Why is this? The variable s1 is an lvalue (it is not temporary, it is addressable, and it can be accessed from multiple parts of the program), so shouldn't the call resolve to string& (a simple lvalue reference)? I don't see where the double reference came from. s1 is a value, not a reference, isn't it? Why does this call involve rvalues at all?
In more general terms, I am a bit confused as to when template parameters resolve to T& && (an rvalue reference to an lvalue reference?) or T&& & (an lvalue reference to an rvalue reference?).
So, could someone please explain the following:
Why did the call to print_type_and_value(s1) resolve to print_type_and_value(string& &&t) ?
In general, when does f(var) resolve to f(T& &&x) or f(T&& &x) ?
I've seen examples in which template parameters resolve to T&& &&, which looks to me like an rvalue reference to an rvalue reference. When does this happen?
Of course, I am aware of the reference collapsing rules, and I understand that T& & is collapsed to T&, but I'm wondering why the call in this example resolved to T& && in the first place.
Thanks in advance for your help!
Edit:
I understand the basics of reference collapsing, but one thing that I'd like to know is why this specific example behaved in the way it did.
Why did print_type_and_value(s1) resolve to print_type_and_value(string& &&t) and then collapse to print_type_and_value(string& t) ?
Edit 2:
Thanks a lot for your links! I'm starting to understand it.
I just have one more question. Why does the template type evaluate to string& when a variable of type string is passed?
Edit 3:
I've re-read the links you've posted, and I 100% get It now. Thanks again!
The reference collapsing rule make print_type_and_value<string&>(string& &&t) equivalent to print_type_and_value<string&>(string& t): there are no reference to reference.
Here is an excellent question/answer on SO regarding this rule.