where and when an xvalue is used?
Asked Answered
S

1

9

I read the standard on xvalue. It is closely associated with rvalue reference expression. But when programming, I really cannot find scenarios that needs xvalue.

For example: function myClass&& f() return an rvalue reference. So expression f() is xvalue. But how is this useful? How can I make use of this technique in programming?

If another function myClass g() is defined, what is the difference between g() and f()?

Under what circumstances should I use f() and xvalue instead of g() prvalue?

Sandy answered 30/6, 2013 at 3:27 Comment(4)
see moveGibbons
sort of like your question, xvalueSpicy
@Spicy I have read this post. But it only copies the standard text from which I don't get the idea.Sandy
See https://mcmap.net/q/152386/-is-there-any-case-where-a-return-of-a-rvalue-reference-amp-amp-is-useful/951890Sirajuddaula
O
11

In your example, you defined a function g which returns a prvalue, and a function f which returns an xvalue.

What these have in common is that if you pass the result of the function call, g() and f() respectively, to another function h(...), they will both prefer the overload of h that takes an rvalue as argument (if there are multiple overloads of h).

The difference between g() and f() is that g() has no dynamic type (i.e. the dynamic type is the same as the static (=declared) type), whereas f() has dynamic type (possibly different from the static type), in other words, if it's defined to return an rvalue reference to a base class, it will enable polymorphic behaviour. (It's this characteristic that xvalues share with lvalues, which is why xvalues and lvalues together form the larger group of glvalues, whereas prvalues are only rvalues.)

For examples of use cases, I'll just refer to this existing question: Is there any case where a return by rvalue reference is useful.

Ouse answered 30/6, 2013 at 3:53 Comment(5)
jogojapan, what do you mean by "the dynamic type is the same as the static (=declared) type"? dynamic type means polymorphism.Sandy
prvalues and xvalues also form the larger group rvalue. xvalue is somewhat special both belonging to glvalue and rvlaue. "(It's this characteristic that xvalues share with lvalues", could you explain what do they share?Sandy
@Zack The dynamic type is the type of the value of an expression at run-time. Prvalues also have a dynamic type, but it's always the same as the static type, i.e. the type given to it at compile time in the declaration (see definition 1.3.8 in §1.3 of the C++11 Standard). But indeed it's common to say "they don't have dynamic" type when, strictly speaking, all we mean is that the dynamic type is the same as the static one.Ouse
@Zack Regarding the second question: What lvalues and xvalues share is the characteristic that they have a dynamic type (in the sense that the dynamic type can be different from the static type). This is characteristic of xvalues and lvalues (i.e. of all glvalues), but pure rvalues (prvalues) do not have a dynamic type (that can be different from the static type).Ouse
I struggle for some time to understand the difference between xvalue and prvalue. Finally, I found this nice article by Bjarne Stroustrup here, which discussed also how the names are created. The difference between xvalue and prvalue is that xvalue has identity and prvalue does not. I think this is pretty much the same as saying xvalues are (rvalue) references (so they reference other objects and have identities) bound to rvalues (so that are movable) while prvalues are rvalues that are not references. Is this idea correct?Janitajanith

© 2022 - 2024 — McMap. All rights reserved.