临时结构是 r 值还是 x 值?

Is a temporary struct an r-value or an x-value?

我想要有关 C++ 值类别的说明。

struct Foo {...};
void do_something(Foo{});

Foo{} 是在 r 值还是 x 值之上?

我了解值类别存在层次结构,并且 r 值实际上是 x 值或 pr 值。我也知道标准说“临时物化是一个 x 值”,但我不确定创建临时物是否符合这个定义。

但我不确定的是 gl-value 和 r-value 是否是层次结构中的“抽象”类别,叶子(l-value、x-value 和 pr-value)是实际实施。

有人可以为我解释一下吗?

来自 https://eel.is/c++draft/basic.lval(2021 年 4 月 2 日访问):

Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is called its value category.

这回答了您关于 gl 值和 r 值是否是值类别的问题:它们是。

关于你的具体情况是什么样的价值,让我们看看xvalue:

  • An xvalue is a glvalue that denotes an object whose resources can be reused (usually because it is near the end of its lifetime).

[ ... snip ... ]

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

  • (4.1) the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to object type ([expr.call]),
  • (4.2) 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]),
  • (4.3) a subscripting operation with an xvalue array operand ([expr.sub]),
  • (4.4) 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
  • (4.5) 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]).

您的案例似乎不符合这些条件。现在让我们看看纯右值:

  • A prvalue is an expression whose evaluation initializes an object or computes the value of an operand of an operator, as specified by the context in which it appears, or an expression that has type cv void.

您的案例似乎是在初始化一个对象。因此,我会说它是纯右值。