I wrote some test code.
int getInt() {
int a = 3;
return a;
}
int& getIntR() {
int a = 3;
return a;
}
int getRL() {
return int(1);
}
int&& getRRL() {
return getRL();
}
int main() {
// works, even though there may cause some problem
int& d = getIntR();
// right,reference to a temporary variable
int&& e = getInt();
// right,because the returned value is lvalue which can't be assigned to rvalue referecen
//int&& f = getIntR();
// here is the code which confused me
int& g = getRRL();
int&& ee = 10;
int& e = ee;
}
I construct a function returns a int&& type, so I need a temporatory variable. as getRRL() show, I think the code maybe treated like this.
int&& temp = 1;
int&g = temp;
temp is a reference to rvalue, but it itself is a lvalue, so i can assign this to int&, but the conpiler says "the initial value of of a non-const refernce must be lvalue" which means the returned type is not lvalue. But the code below works well.
int&& ee = 10;
int& e = ee;
Can someone tell me the reason? Thanks very much!