Constant as rvalue in C++(11)

前端 未结 4 1372
孤城傲影
孤城傲影 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 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)

提交回复
热议问题