Lvalues and Rvalues
作者:互联网
To complement the notion of an lvalue, we have the notion of an rvalue. Roughly, rvalue means ‘‘a
value that is not an lvalue,’’ such as a temporary value (e.g., the value returned by a function).
If you need to be more technical (say, because you want to read the ISO C++ standard), you
need a more refined view of lvalue and rvalue. There are two properties that matter for an object
when it comes to addressing, copying, and moving:
• Has identity: The program has the name of, pointer to, or reference to the object so that it is
possible to determine if two objects are the same, whether the value of the object has
changed, etc.
• Movable: The object may be moved from (i.e., we are allowed to move its value to another
location and leave the object in a valid but unspecified state, rather than copying; §17.5).
It turns out that three of the four possible combinations of those two properties are needed to pre-
cisely describe the C++ language rules (we have no need for objects that do not have identity and
cannot be moved). Using ‘‘ m for movable’’ and ‘‘ i for has identity,’’ we can represent this classifi-
cation of expressions graphically:
lvalue { i&!m }
xvalue { i&m }
glvalue { i }
prvalue { !i&m }
rvalue { m }
So, a classical lvalue is something that has identity and cannot be moved (because we could exam-
ine it after a move), and a classical rvalue is anything that we are allowed to move from. The other
alternatives are prvalue (‘‘pure rvalue’’), glvalue (‘‘generalized lvalue’’), and xvalue (‘‘x’’ for ‘‘ex-
traordinary’’ or ‘‘expert only’’; the suggestions for the meaning of this ‘‘x’’ hav e been quite imagi-
native). For example:
void f(vector<string>& vs)
{
vector<string>& v2 = std::move(vs);
// ...
}
// move vs to v2
Here, std::move(vs) is an xvalue: it clearly has identity (we can refer to it as vs ), but we have explic-
itly given permission for it to be moved from by calling std::move() (§3.3.2, §35.5.1).
标签:rvalue,object,lvalue,move,vs,value,Rvalues,Lvalues 来源: https://blog.csdn.net/Damon_X/article/details/117908841