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

How expressions designating temporary objects are xvalue expression?

cppreference 开始,我试图理解产生 xvalues 的表达式,最后得到了这个摘要:

The following expressions are xvalue expressions:

  • ...
  • any expression that designates a temporary object, after temporary materialization.

Temporary materialization 是:

A prvalue of any complete type T can be converted to an xvalue of the same type T. This conversion initializes a temporary object of type T from the prvalue by evaluating the prvalue with the temporary object as its result object, and produces an xvalue denoting the temporary object.

并且根据我对上述引用的理解,临时物化涉及将纯右值转换为亡值以初始化创建的临时;这意味着只要 prvalue 被具体化,就会出现一个 xvalue 表达式。所以我发现自己必须了解纯右值具体化的时间。然后我从 cppreference:

检查了 this

Temporary materialization occurs in the following situations:

  • 1- when binding a reference to a prvalue;
  • 2- when performing a member access on a class prvalue;
  • 3- when performing an array-to-pointer conversion or subscripting on an array prvalue;
  • 4- when initializing an object of type std::initializer_list from a braced-init-list;
  • 5- when typeid is applied to a prvalue
  • 6- when sizeof is applied to a prvalue
  • 7- when a prvalue appears as a discarded-value expression.

Note that temporary materialization does not occur when initializing an object from a prvalue of the same type (by direct-initialization or copy-initialization): such object is initialized directly from the initializer. This ensures "guaranteed copy elision".

任何人都可以用简单的例子帮助我说明在情况 3、4 和 7 中如何涉及 xvalue 表达式。

situation 3, 4 and 7.

7(废弃表达式)最简单:

42; // materialize and discard
std::string{"abc"}; // materialize and discard

3(处理数组右值)需要知道如何制作它们

using arr_t = int[2][3];
int a = arr_t{}[0][0]; // have to materialize to be able to subscript

4(制作一个 std::initializer_list)是罐头上的字样

std::initializer_list<std::string>{
  "abc"s,
  "def"s
}; // have to materialize the two strings
   // to place them in the secret array pointed to by the list