often used seldom defined terms: lvalue

2020-01-25 06:19发布

What is an lvalue?

8条回答
萌系小妹纸
2楼-- · 2020-01-25 07:15

One of the best explanations I know of can be found in this article on RValue references.

another way to determine whether an expression is an lvalue is to ask "can I take its address?". If you can, it's an lvalue. If you can't, it's an rvalue. For example, &obj , &*ptr , &ptr[index] , and &++x are all valid (even though some of those expressions are silly), while &1729 , &(x + y) , &std::string("meow") , and &x++ are all invalid. Why does this work? The address-of operator requires that its "operand shall be an lvalue" (C++03 5.3.1/2). Why does it require that? Taking the address of a persistent object is fine, but taking the address of a temporary would be extremely dangerous, because temporaries evaporate quickly.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-01-25 07:21

A simple example of what is definitly not an lvalue:

3 = 42;
查看更多
登录 后发表回答