++x returns lvalue but x++ return rvalue



 DEVELOP > c-Plus-Plus > ++x returns lvalue but x++ return rvalue

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Chris Mantoulidis"
Date: 28 Dec 2003 06:32:09 AM
Object: ++x returns lvalue but x++ return rvalue
Why is that? Why does ++x return lvalue and x++ return rvalue?
for example, this will work
++x = 10;
it will set x to 10...
but this won't, cuz x++ is rvalue:
x++ = 10; //ERROR
TIA,
cmad
.

User: "Tim Threlfall"

Title: Re: ++x returns lvalue but x++ return rvalue 28 Dec 2003 09:05:15 AM
"Chris Mantoulidis" <cmad_x@yahoo.com> wrote in message
news:a8587dd9.0312280432.3ff032f@posting.google.com...

Why is that? Why does ++x return lvalue and x++ return rvalue?

for example, this will work

++x = 10;

it will set x to 10...


but this won't, cuz x++ is rvalue:

x++ = 10; //ERROR

TIA,
cmad

It should be noted that ++x=10; is undefined for inbuilt types, as stated in
section 5.0.4 of the standard:
Between the previous and next sequence point a scalar object shall have its
stored value modified at most once by the evaluation
of an expression. Furthermore, the prior value shall be accessed only to
determine the value to be stored.
The requirements of this paragraph shall be met for each allowable ordering
of the subexpressions of a full
expression; otherwise the behavior is undefined. [Example:
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented
-end example]
.

User: "Rob Williscroft"

Title: Re: ++x returns lvalue but x++ return rvalue 28 Dec 2003 09:03:52 AM
Chris Mantoulidis wrote in
news:a8587dd9.0312280432.3ff032f@posting.google.com:

Why is that? Why does ++x return lvalue and x++ return rvalue?

for example, this will work

++x = 10;

Well it might do what you expect, but it introduces undefined
behaviour (aka UB) as it modifies x twice without an intervening
sequence point. So don't do it.
Note if x is a User Defined Type (aka UDT) then since operator ++()
is a function call, there is an intervening sequence point and the
code is ok, but then the result of ++x is what ever operator ++()
returns which isn't nessaseraly an lvalue.


it will set x to 10...

or 11 or any possible value or it may cause a processor exception
or <insert your favourate UB nonsence here>.


but this won't, cuz x++ is rvalue:

x++ has to be an rvalue as it has a different actual (post sequence
point) value than x (i.e. x - 1).

x++ = 10; //ERROR

void f( int & y )
{
y = 10;
}
int main()
{
int x = 1;
f( ++x );
}
The above is ok as the function call f() intoduces a sequence point,
i.e. x is updated before the call is made.
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
.

User: "Chris Mantoulidis"

Title: Re: ++x returns lvalue but x++ return rvalue 29 Dec 2003 01:00:30 AM
Thanks all for the replies. It all seems clear now.
.

User: "Severin Ecker"

Title: Re: ++x returns lvalue but x++ return rvalue 28 Dec 2003 07:05:42 AM
hi!

but this won't, cuz x++ is rvalue:

x++ = 10; //ERROR

thats because the post-increment operator returns a temporary with the value
of x before the increment, not x itself.
on the other side the pre-increment operator increments x and then returns
x.
regards,
sev
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER