Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wanted a clarification regarding C++ value categories.

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

Is the Foo{} above a r-value or an x-value?

I understand that there is a hierarchy of value categories, and that r-values are actually either an x-value or a pr-value. I also know that the standard says that "temporary materialization is an x-value" but I wasn't sure if the creation of a temporary fits under this definition.

But what I wasn't sure about was whether gl-value and r-value were "abstract" categories in the hierarchy, with the leafs (l-value, x-value, and pr-value) being the actual implementations.

Could someone explain this for me?

question from:https://stackoverflow.com/questions/66047692/is-a-temporary-struct-an-r-value-or-an-x-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

From https://eel.is/c++draft/basic.lval (accessed 04/02/2021):

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.

This answers your question of whether gl-value and r-value are categories of values: they are.

On what kind of value your particular case is, let's look at 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]).

Your case does not appear to fit any of those. Now let's look at prvalue:

  • 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.

Your case appears to be initialising an object. For this reason, I would say it's a prvalue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...