Temporary materialization and xvalue expression

430 Views Asked by At

cppreference is said that: any expression that designates a temporary object after temporary materialization is xvalue expression (since C++17).

Temporary materialization is:

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.

What I suspect that I am not understood is that, from this quote, the temporary materialization conversion (prvalue->xvalue) produces an xvalue denoting to the temporary object. To clear what I am not fully understanding, I will provided a simple example:

const T& t = T(); 

The reference t is bound to prvalue T(). So a temporary of type T gets created and initialized from this prvalue; then the reference will bind to that temporary. I think the temporary looks like this: and t reference will bind to it:

T __tmp{ };
const T& t = __tmp; 

Indeed the above two lines are happened implicitly, but in general, can I said that the expression __tmp in the assignment const T& t = __tmp; is xvalue expression?

Till now I am assuming I was true, if not, what does cppreference mean by "any expression that designates a temporary object, after temporary materialization is xvalue expression"? where's exactly the xvalue expression now? what does the word "designates" means in this context?

2

There are 2 best solutions below

1
On

can I said that the expression __tmp in the assignment const T& t = __tmp; is xvalue expression?

It would be better to say that the code you showed as equivalent is incorrect because it doesn't include the xvalue conversion step. A more accurate version would be:

T __tmp{ };
const T& t = std::move(__tmp);

The std::move cast results in an xvalue expression referring to the object __tmp.

what does the word "designates" means in this context?

The same thing it means in other contexts. It isn't being used as a special term. It's just the word "designate": to mark or point out; indicate; show; specify. The expression T() indicates/shows/specifies a particular object.

0
On

When the compiler converts const T& t = T(); into the AST, it looks roughly like this:

`-DeclarationStatement
  `-VariableDeclaration (name: t, type: const T&)
    `-InitializerExpr (type: const T, category: glvalue)
      `-MaterializeTemporaryExpr (type: const T, category: xvalue) [conv.rval]
        `-ImplicitCastExpr (type: const T, category: prvalue) [dcl.init.ref#5.3]
          `-ExplicitCastExpr (type: T, category: prvalue) [expr.type.conv]

(the ImplicitCastExpr is from the words "adjusted to" in dcl.init.ref#5.3.sentence-2)

so to answer

where's exactly the xvalue expression now?

it's the MaterializeTemporaryExpr on the syntax tree. There are many such implicit conversions in C++ (and even in C, e.g. consider the AST of 1.2/2

can I said that the expression __tmp in the assignment const T& t = __tmp; is xvalue expression

no; that's lvalue per expr.prim.id and so it takes a very different path through [dcl.init.ref]. Also there is no assignment