Rvalue Reference is Treated as an Lvalue?

后端 未结 4 1438
小蘑菇
小蘑菇 2020-11-22 09:20

I posted this answer: https://stackoverflow.com/a/28459180/2642059 Which contains the following code:

void foo(string&& bar){
    string* temp = &         


        
4条回答
  •  再見小時候
    2020-11-22 09:54

    The expression bar is an lvalue. But it is not an "rvalue reference to string". The expression bar is an lvalue reference. You can verify it by adding the following line in foo():

    cout << is_lvalue_reference::value << endl; // prints "1" 
    

    But I agree with the rest of the explanation of Angew.

    An rvalue reference, after it has been bound to an rvalue, is an lvalue reference. Actually it is not just for function parameters:

    string&& a = "some string";
    string&& b = a; // ERROR: it does not compile because a is not an rvalue
    

    If you can perform any operation on an rvalue reference that you can on an lvalue reference what is the point in differentiating between the two with the "&&" instead of just an "&"?

    An rvalue reference allows us to do some "lvalue operations" before the rvalue expires.

提交回复
热议问题