问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/9670954/are-temporary-objects-xvalues