0

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!

Azure
  • 1
  • 2
  • `int& g = getRRL();` does not compile. Which compiler are you using btw? VS has some extensions, but they are not standard compliant. – vsoftco Sep 21 '15 at 04:15
  • This question could be useful: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](http://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues). `xvalue` related topics, to be precise. This is not a duplicate, though, since it does not answer your question directly. – awesoon Sep 21 '15 at 04:54

0 Answers0