C++0x新概念:glvalue, xvalue, prvalue(泛左值,x值,纯右值)

以下内容摘自最新的C++0x草案

All expressions are now divided into three "value categories":

* "lvalues" are the same as what's meant traditionally by lvalue.
* "xvalues" are objects created by rvalue reference operations (sometimes previously called "rvalue reference objects"). The "x" can be for "eXpiring value" or a cross between lvalues and rvalues.
* "prvalues" are the new name for traditional rvalues (i.e., excluding the xvalue cases). The "p" is for "pure rvalue".

There are two compound categories:

* "glvalues" are lvalues and xvalues. These are things that have dynamic type and object identity.
* "rvalues" are xvalues and prvalues. These are (as now in the draft) things that are potentially movable.

... named rvalue references are treated as lvalues and unnamed rvalue references to objects are treated as xvalues; rvalue references to functions are treated as lvalues whether named or not.

 表达式根据其值的类型可分为以下三类:

  • lvalue:左值,即传统意义上的左值。
  • xvalue(expiring value):x值(中间值?),指通过“右值引用”产生的对象。
                                     这里x可以理解为即将消失(expiring),也可理解为中间(横跨左值和右值)。
  • prvalue(pure rvalue):纯右值,即传统意义上的右值。
两种复合类型:
  • glvalue(general lvalue):泛左值,由左值和x值构成。泛左值具有动态的类型和对象属性。
  • rvalue:右值,由x值和纯右值构成。右值具有潜在的可移动性。
关于右值引用:
  • 具名右值引用被视为左值。
  • 无名右值引用被视为x值。
  • 对函数的右值引用无论具名与否都将被视为左值。
struct A {
  int m;
};
A&& operator+(A, A);
A&& f();
A a;
A&& ar = static_cast<A&&>(a);
这里表达式 f(), f().m, static_cast<A&&>(a), 以及 a + a 都是 x值。
而表达式 ar 为 A 类型的左值。

你可能感兴趣的:(C++,c,struct,object,reference)