Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are temporary objects xvalues?

I am currently writing my degree dissertation and it involves also some explaining of the theory behind C++11 which is really fine as C++ is my programming language of choice and the standard is more or less available for free (N3337) to get yourself lost in.

Yet I have hit a wall while trying to explain the new xvalue category accurately and in detail. It is my understanding that a temporary object is always a xvalue but I cannot find any reference to this in the standard. It is my understanding that the value category of an expression of a function call to a function that has a non-reference return type, is a xvalue. The standard says that "a xvalue is the result of certain kind of expressions that involve rvalue-references" which is bugging me. For example:

TestClass { ... };
testClass createObject() { return testClass(); }

void someFunction(TestClass& testClass) { ... }
void someFunction(TestClass&& testClass) { ... }

someFunction(createObject());

The above will, as expected, call the overloaded function which takes an rvalue-reference as parameter. Yet createObject() does not return a rvalue-reference, it returns a temporary object of type TestClass. My problem is now, I have to explain the reasons behind it. What does the expression "createObject()" evaluate to? If it is indeed a xvalue because it returns a temporary object, the reasoning behind it is clear and the rvalue-reference is favored during overload resolution. If it does not, what is the explanation for this behaviour with regard to the standard? Is there some implicit conversion logic defined somewhere that I haven't found yet?

I'd really appreciate if someone could help me out on this one because even after days of digging and reading, I have yet to come up with a sound explanation. Thanks a lot in advance.

like image 975
khaos Avatar asked Mar 12 '12 16:03

khaos


People also ask

What are temporary objects C++?

A temporary object is an unnamed object created by the compiler to store a temporary value.

What are rvalues?

An rvalue (so-called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object. A prvalue (“pure” rvalue) is an rvalue that is not an xvalue.

What is prvalue in c++?

Lvalues and Rvalues (C++) A glvalue is an expression whose evaluation determines the identity of an object, bit-field, or function. A prvalue is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as specified by the context in which it appears.


1 Answers

Objects are never {l|r|x}values. value categories describe expressions.

xvalue is the value category of the function call expression where the function return type is an rvalue reference to object (e.g. std::move), and it is also the value category of the cast expression where the cast is to an rvalue reference to object (e.g. the guts of std::move).

The function call expression createObject() in your example is an prvalue expression because it is a function call to a function with non-reference return type.

like image 112
Cubbi Avatar answered Oct 20 '22 02:10

Cubbi