By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

lvalue and assignment to temporary object

P: n/a
Consider the following:
int x;
int y;
int z;
(x+y) = z;

For this statement, I get the following error with g++ compiler:
error: non-lvalue in assignment

Suppose I have a class Test and x, y, z are objects of type Test.
Suppose I have
Test Test::operator+(const Test& ref);
Test operator+=(Test lhs, Test rhs);

Given this, the following are accepted by g++:
(x+y) = z;
(x+y) += z;

My question:
Is (x+y) an lvalue ?

I thought, x+y being a temporary object, we cannot take its address
and so not an lvalue. However the compiler accepts it on the left hand
side of the assignment operator. I am unable to understand this.

Kindly clarify

Thanks
V.Subramanian
Dec 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Dec 21, 7:18 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the following:
int x;
int y;
int z;
(x+y) = z;
For this statement, I get the following error with g++ compiler:
error: non-lvalue in assignment
Correct.
Suppose I have a class Test and x, y, z are objects of type Test.
Suppose I have
Test Test::operator+(const Test& ref);
Test operator+=(Test lhs, Test rhs);
Given this, the following are accepted by g++:
(x+y) = z;
(x+y) += z;
My question:
Is (x+y) an lvalue ?
No. But the = here isn't really an operator, either; it's more
a particular syntax of a function call. (Overloaded "operators"
are only partially operators---they have the syntax of
operators, but semantics of a function call.)

Since you can call a member function on an lvalue, the above is
legal.
I thought, x+y being a temporary object, we cannot take its
address and so not an lvalue.
That would be too simple. x+y is a temporary object (an
rvalue), and *you* cannot take its address. rvalues of class
types do have addresses, however, and the compiler can get at
the address if it needs to, for example to call a member
function.

(Technically, you cannot take its address is only partially
true. You can not apply the built-in operator & on it, since
the built-in operator & requires an lvalue, which it is not. If
the class overloads operator&, however, we're back to the above
rule: the overloaded operator is a function call of a member
function, which is legal.)
However the compiler accepts it on the left hand side of the
assignment operator. I am unable to understand this.
It is confusing. There are several different concepts involved
simultaneously, and there is also the fact that rvalue means
something different for class types than for non-class types.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.