I have been carefully studying rvalues and rvalue references, in detail, for a number of weeks. I have growing confidence that I have some clarity in understanding the difference between:
int x;
vs. int && x = …;
A&& foo()
, and given an expression consisting solely of foo()
, the expression category is "xvalue")foo
, the type of a subexpression foo()
when used within a containing expression is A
(not A&&
)).My question regards the difference between xvalue expressions and prvalue expressions (both of these expression categories being rvalues).
Given:
class A
{};
A&& foo() {…}
A goo() {…}
void test(A&& a) {…}
int main()
{
test(foo()); // test() called with xvalue expression
test(goo()); // test() called with prvalue expression
}
Note that the function test()
is successfully called with both an xvalue expression, and a prvalue expression. (Please correct me if I have a misunderstanding about this.)
My question is: Since the function parameter A&& a
to test
successfully binds to both xvalues and to prvalues, what is the practical difference between xvalues and prvalues in terms of overload resolution?
Is there an example of a scenario in which the xvalue/prvalue nature of an expression results in different function overload resolution? Or is the distinction between xvalues and rvalues relevant only for other aspects of the language, independent of function overload resolution (for example, the result of decltype
(i.e., see What is decltype(0 + 0)?))?
I don't believe there would be any difference in overload resolution, no.
There are plenty of other reasons to clarify what expressions are xvalues and prvalues, though. Xvalues are modifiable; prvalues are not. Xvalues are subject to the rules against improper uses of glvalues before or after the object's lifetime; this is never an issue with prvalues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With