c++ - C++ 中的 xvalue 是什么

标签 c++ c++17 xvalue

我试图了解值(value)类别的差异,我发现 this document由微软开发。

enter image description here

- 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?他们为什么不说xvalue是左值或右值?在我看来,这个措辞是有意传达某种信息,但我不明白为什么

最佳答案

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

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

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

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

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

仅定义了左值和右值。请参阅前面提到的避免循环定义。

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]).

关于c++ - C++ 中的 xvalue 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58703140/

相关文章:

c++ - 指定临时对象的表达式如何是xvalue表达式?

c++ winrt uwp 如何从依赖属性中获取值

c++ - 如何正确存储中文字符串?

c++ - 当一个 xvalue 绑定(bind)到一个 const 左值引用时,它的生命周期是否会延长?

c++ - 包含和前向声明都出错

c++ - 根据模板参数添加成员函数和成员变量

c++ - `xvalue` C++11/14 中的行为

c++ - 如何在 Windows 上使用 QtCreator 为 Linux 构建和调试应用程序?

c++ - 在 C++ 中创建一类用户定义大小的对象

c++ - 奇怪的 mingw 链接器错误与 boost ?