473,411 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

++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
Jul 22 '05 #1
4 4959
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
Jul 22 '05 #2
Chris Mantoulidis wrote in
news:a8*************************@posting.google.co m:
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/
Jul 22 '05 #3

"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8*************************@posting.google.co m...
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]


Jul 22 '05 #4
Thanks all for the replies. It all seems clear now.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
11
by: JKop | last post by:
The following compiles: // syrup.cpp struct DoubleInDisguise { double data; };
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
9
by: Kavya | last post by:
These were the questions asked to me by my friend 1. Operator which may require an lvalue operand, yet yield an rvalue. 2. Operator which may require an rvalue operand, yet yield an lvalue. ...
14
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
6
by: Yarco | last post by:
I've alway thought lvalue means Left Value and rvalue means Right Value before i've read someone's article. It is said "lvalue = location value" and "rvalue = read value". Which one is right, then?
9
by: usao | last post by:
Does anyone have an example of how to create a class which describes and array, such that I can use the subscript operator on both the left and right side of an assignment statement? This is as...
2
by: Chad | last post by:
The following question actually stems from an old Chris Torek post. And I quote from the following old CLC url ...
2
by: Chad | last post by:
I have the folllowing #include <stdio.h> int main(void) { int x = 1, y = 2; int *ip; ip = &x;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.