tags:

views:

168

answers:

3

Somebody generalized the statement "Temporaries are rvalues". I said "no" and gave him the following example

 double k=3;

 double& foo()
 {
    return k;
 }

 int main()
 {

     foo()=3; //foo() creates a temporary which is an lvalue
 }

Is my interpretation correct?

+7  A: 

No. You are returning a reference to an global double, not a temporary.

The same test with a real temporary would be:

double foo() { return 3.0; }
int main() {
   foo() = 2.0; // error: lvalue required as left operand of assignment
}

EDIT: The answer was meant just to identify that the example was wrong, and I did not really want to get into the deeper discussion of whether temporaries are or not rvalues... As others have said, lvalue-ness or rvalue-ness are properties of an expression and not of the object (in the most general sense, not only class instances). Then again, the standard says that:

§3.10/5 The result of calling a function that does not return a reference is an rvalue. User defined operators are functions, and whether such operators expect or yield lvalues is determined by their parameter and return types.

§3.10/6 An expression which holds a temporary object resulting from a cast to a nonreference type is an rvalue (this includes the explicit creation of an object using functional notation (5.2.3)).

Which AFAIK are the circumstances under which temporaries are created. Now, it is also true that you can bind a constant reference to a temporary, in which case you will get a new variable (the reference) that can be used as an lvalue that effectively refers to the temporary object.

The fine line is that expressions that create temporaries are rvalue expressions. You can bind a constant reference to the result of that expression to obtain a variable that can be used as an const-qualified lvalue expression.

David Rodríguez - dribeas
@David: As far as I know, nameless _objects_ are temporaries. In this case a nameless object is created when `foo()` is called, so how can you say that it is not a temporary?
Shekhar Gupta
@user Scalar rvalues denote values, not objects. There is no such thing as a temporary double object.
FredOverflow
@user242265: in your example, there is no nameless object; `foo()` returns a reference to `k`, which is an lvalue. In David's example, the result of `foo()` is an rvalue, so you can't assign to it.
Mike Seymour
@FredOverflow: Then that means "temporaries are rvalues" makes some sense?
Shekhar Gupta
@user242265 you are not returning nameless object but rather a reference to named object. No object is being created by foo().
doc
@user See my own answer, it does not make any sense.
FredOverflow
@Mike Seymour: if you return a class instance instead of a primitive object, you *can* assign to it.
Philipp
+8  A: 

Temporaries and rvalues are different (but related) concepts. Being temporary is a property of an object. Examples of objects that aren't tempory are local objects, global objects and dynamically created objects.

Being an rvalue is a property of an expression. The opposite of rvalues are lvalues such as names or dereferenced pointers. The statement "Temporaries are rvalues" is meaningless. Here is the relationsip between rvalues and temporary objects:

An rvalue is an expression whose evaluation creates a temporary object which is destroyed at the end of the full-expression that lexically contains the rvalue.

Note that lvalues can also denote temporary objects!

void blah(const std::string& s);

blah(std::string("test"));

Inside the function blah, the lvalue s denotes the temporary object created by evaluating the expression std::string("test").

Your comment "references are lvalues" is also meaningless. A reference is not an expression and thus cannot be an lvalue. What you really mean is:

The expression function() is an lvalue if the function returns a reference.

FredOverflow
doc
Temporaries are rvalues, a constant reference bound to a temporary is an lvalue. Both §3.10/6 and §5.2.3/1 state that `std::string("test")` is an rvalue. There is a really fine line there...
David Rodríguez - dribeas
@David Rodríguez - dribeas: this is my humble opinion. Terms rvalue and lvalue orginated from C (or maybe even Fortran?), long time ago. They were used during assignment operations and in my opinion it's really wrong to redefine them by conclusion derived from their primary definition and a case of use.
doc
@doc: the comment was on the answer, not to your comment. Getting to your comment, technically and as Fred correctly states, `s` used as an expression is a *constant-qualified lvalue*. In the standard *lvalue* and *rvalue* expressions are never defined perse, but rather through the description of properties like: an *lvalue* refers to an object or function, some *rvalues* also refer to an object or function... At any rate this answer is right in that Xvalueness is a property of an expression and that you can have expressions that refer to temporaries but are lvalues.
David Rodríguez - dribeas
@doc: The terms are defined by the standard which clearly states that the two sides of an assignment are nothing more than a historical artifact: "An lvalue (so called, **historically**, because lvalues **could** appear on the left-hand side of an assignment expression) [...]" An example of an lvalue that cannot appear on the left-hand side of an assignment is `some_array_name = blah`. An example of an rvalue on the left-hand side of an assignment is `std::string("hello") = blah`. C++0x introduces a third expression category called *xvalue*, and surely an assignment does not have three sides.
FredOverflow
@David: Temporaries are neither lvalues nor rvalues because objects are not expressions. A single object can be denoted by multiple expressions which may be lvalues or rvalues. In my example, `std::string("test")` is an rvalue and `s` is an lvalue that both denote the same temporary object.
FredOverflow
@FredOverflow - as I stated above this is my humble opinion. Now we it becomes philosophical argumentation. You prefer knowledge, I prefer understanding. You believe thaty it's better to keep on tracks, I believe that it's better to question everything. Discussion is worthless, if you won't consider other points of view.
doc
A: 

Temporaries were so consistently protected from becoming lvalues, that they are now called rvalues. But C++0x will allow temporaries to become lvalues thanks to move semantics. Like in this dumb snippet

void blah(ICanBeTemporary && temp)
{
    temp.data = 2; //here temporary becomes lvalue
}


//somewhere
blah(ICanBeTemporary("yes I can"));

Now we have terminology mess. People used to call temporaries rvalues and this is called rvalue reference. Named objects are now considered to be non-rvalue referenced.

doc
Under no circumstance does an object become an lvalue or an rvalue. It's just that multiple expressions can denote the same (temporary) object, and each of those expressions can be an lvalue or an rvalue.
FredOverflow
@FredOverflow: but temporaries are called rvalues - and the above is called *rvalue reference* - reference to what? expression!? or rather an object? Then what *rvalue* really denotes?
doc
References always refer to objects. It's just that certain kinds of references can only be *syntactically initialized by* certain expressions. This does not imply a reference *refers to* an expression. At runtime, there is no such thing as an lvalue or an rvalue.
FredOverflow
@FredOverflow: at runtime there's no such thing as expression. Yet you can still apply terms lvalue, rvalue to machine instructions operands. Anyway, if term *rvalue reference* is used and reference implies an object, then rvalue denotes an object also. It's classical transitive law of logic. Currently object becomes rvalue. If you would question legality of term *rvalue reference*, then it would be another pair of shoes.
doc
"Yet you can still apply terms lvalue, rvalue to machine instructions operands." -> **No you can't**. There is no such thing as a machine instruction in the C++ standard. The terms *lvalue* and *rvalue* have very specific meanings in C++. They have absolutely nothing to do with machine instructions.
FredOverflow
@FredOverflow: Who said ther is? **Please, do not take my words out of context**. Of course there's no such thing as machine instruction in C++ :/. Hence I don't know why you mentioned something about runtime. But as you claimed so, I replied that you can apply terms lvalue. rvalue to machine instructions at runtime. After all, every expression is translated into a set of machine instructions. If you call expression rvalue or lvalue, then it is logical that related set of machine instructions may also be called rvalue or lvalue.
doc