Constant as rvalue in C++(11)

前端 未结 4 1316
孤城傲影
孤城傲影 2021-02-20 13:33

Why const int is not an R-value in C++(11)? I thought that R-value was \'anything\' which cannot be on the left hand side and constants fulfil that

相关标签:
4条回答
  • 2021-02-20 13:52

    The parameter type is an rvalue reference and the standard forbids initialization of an rvalue-reference with an lvalue of a "reference-related" type.

    8.5.3 References [dcl.init.ref ]

    ...

    5 ... If T1 is reference-related to T2 and the reference is an rvalue reference, the initializer expression shall not be an lvalue.

    Hence, the following is an error:

    void f (long &&);
    
    const long x = 1;
    void g () { f (x); }
    

    But the following is not:

    void f (long &&);
    
    const int x = 1;
    void g () { f (x); }
    

    So, the reason for the error is not simply "the initialization is not with an rvalue", but because "the initialization is with an lvalue of a reference-related type."

    0 讨论(0)
  • 2021-02-20 14:00

    In the simple words, you should be able to 'steal' the value of rvalue. This is the whole point of different treatment of rvalues. So rvalue should be unnamed-going-to-die-in-near-future-something.

    0 讨论(0)
  • 2021-02-20 14:06

    I thought that R-value was 'anything' which cannot be on the left hand side [of an assignment operaton]

    That's the C++03 definition of rvalue, and even then its a colloquialism and not universally true.

    In C++11, the definitions for lvalue and rvalue have changed somewhat. The rules are complex, and individual situations are handled on a case-by-case basis in the Standard, but here is a general rule of thumb:

    1. If you can take the address of something, it is an lvalue
    2. If the type of an expression is an lvalue reference, that expression is an lvalue
    3. If neither of the above apply, it is an rvalue

    In your particular case, you can take the address of x (eg, it "has a name") and it is therefore an lvalue.

    You can read more about lvalues and rvalues in C++11 in two excellent articles:

    • rvalue References Explained, by Thomas Becker (blog article)
    • Universal References, by Scott Meyers (Overload #111)
    0 讨论(0)
  • 2021-02-20 14:07

    Ok, there are three categories of expressions1:

    1. those that represent objects that have an identity and cannot be moved from;
    2. those that represent objects that have an identity and can be moved from;
    3. those that represent objects that do not have an identity and can be moved from;

    The first ones are called lvalues, the second ones are xvalues, and the third ones are prvalues. If we put lvalues and xvalues together, we have glvalues. Glvalues are all expressions that represent objects with an identity. If we put xvalues and prvalues together we have rvalues. Rvalues are all expressions that represent objects that can be moved.

    The expression in question, x, is a glvalue: one can write &x, so the object clearly has an identity.

    Can we move from this expression? Is this object about to expire? No, it is not. It only expires sometime after the current expression. That means it cannot be moved from. That makes it an lvalue.

    All these names can be a bit confusing because lvalue and rvalue in C++ no longer mean what they meant in their C origins. The C++ meaning is completely unrelated to being on the left or right side of assignment2.

    Personally, I prefer to use the terminology from this paper by Bjarne: iM-values (instead of lvalues), im-values (instead of xvalues), Im-values (instead of prvalues), i-values (instead of glvalues), and m-values (instead of rvalues). That is not the terminology that the standard uses, unfortunately.


    1 Here "have an identity" means "its address can be taken"; "can be moved from" means that it is about to expire, either due to its temporary nature, or because the programmer made that explicit in the type system by calling std::move or something similar.

    2 You can have rvalues on the left side of assignment: std::vector<int>(17) = std::vector<int>(42) is a valid expression, even if it is useless.

    0 讨论(0)
提交回复
热议问题