Question

It took me quite some time to understand the difference between an rvalue and a temporary object. But now the final committee draft states on page 75:

An rvalue [...] is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object.

I can't believe my eyes. This must be an error, right?


To clarify, here is how I understand the terms:

#include <string>

void foo(std::string&& str)
{
    std::cout << str << std::endl;
}

int main()
{
    foo(std::string("hello"));
}

In this program, there are two expressions that denote the same temporary object: the prvalue std::string("hello") and the lvalue str. Expressions are not objects, but their evaluation might yield one. Specifically, the evaluation of a prvalue yields a temporary object, but a prvalue IS NOT a temporary object. Does anyone agree with me or have I gone insane? :)

Was it helpful?

Solution

Yes, i agree with you. This should be fixed in my opinion, and several people i deeply pay respect to have risen the exact same question about this.

OTHER TIPS

This isn't as complicated as it sounds. I am referring to the now finalized standard ISO/IEC 14882-2011. Page 78 says:

An xvalue (an “eXpiring” value) also refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving rvalue references (8.3.2).

The bold above has been added by me. The standard further says:

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

So you only get an xvalue when you are playing around with 'certain kinds of expressions involving rvalue references'. Otherwise your temporary objects are just that - temporary objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top