C++ 中的 xvalues 是什么

What are xvalues in C++

我试图了解值类别的差异,我发现 this document 由 Microsoft 提供。

- An xvalue is a glvalue that [...]
- An lvalue is a glvalue that is not an xvalue.
- An rvalue is a prvalue or an xvalue.

为什么 xvalueglvalue?他们为什么不说 An xvalue is an glvalue or an rvalue?我觉得这个措辞是故意要传达一些东西的,但我不明白为什么

图表可能具有误导性。

有左值、x值和prvalues。它们是排他的和完整的,即每个表达式恰好是左值、xvalue 或 prvalue 之一。

lvalues 和 xvalues 统称为 glvalues
xvalues 和 prvalues 统称为右值

所以 xvalues 既是 glvalues 又是 rvalues。

Why didn't they say An xvalue is an glvalue or an rvalue?

因为右值尚未定义,而且右值将根据 xvalue 定义。必须在不依赖另一个的情况下定义一个,以避免定义变得循环。

还因为 xvalue 既是左值 是右值。

此外,xvalue 是一个不是 lvalue 的 glvalue。更进一步,xvalue 是一个不是 prvalue 的右值。最后,xvalue 是一个既不是左值也不是纯右值的表达式。

I don't understand why they only mentioned gvalues in that sentence

只定义了 glvalue 和 prvalue。请参阅前面提到的避免循环定义。

What are xvalues in C++

嗯,你已经看到它的定义了。该标准有以下非规范性注释,提供了更多信息:

[basic.lval]

[ Note: An expression is an xvalue if it is:

  • the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to object type ([expr.call]),
  • a cast to an rvalue reference to object type ([expr.type.conv], [expr.dynamic.cast], [expr.static.cast] [expr.reinterpret.cast], [expr.const.cast], [expr.cast]),
  • a subscripting operation with an xvalue array operand ([expr.sub]),
  • a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue ([expr.ref]), or
  • a .* pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member ([expr.mptr.oper]).