473,574 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prefix increment/decrement results in lvalue, but postfix one results in rvalue?

Hello experts,

Why can this difference between prefix increment/decrement and
postfix increment/decrement reside in built-in operators for built-in
data types? Thanks.

// test.cpp
//
// >g++ test.cpp
// test.cpp: In function `int main()':
// test.cpp:21: non-lvalue in increment
// test.cpp:22: non-lvalue in decrement
//

#include <iostream>
using std::cout;

int main()
{
int i = 0;

cout << ++++++i;
cout << ------i;
cout << i++++++; // non-lvalue in increment
cout << i------; // non-lvalue in decrement
}

Sep 12 '05 #1
8 6251
Prefix inc/dec changes that contents of the variable immediatley, which
means no additional variables would have to placed on the stack:
C++:
++++++i;
Assembly:
inc i
inc i
inc i

Postfic inc/dec delays changing the value of the variable until the end
of the line...

C++:
i++++++;
Assembly:
push i
mov eax, i
inc eax
push i
mov eax, i
inc eax
push i
mov eax, i
inc eax
pop i
pop i
pop i

if you walk through this logically, you will see that it makes no sense
to do a series of post inc/dec statements. They would just override
themselves when the pop instructions are called... so the compiler
doesn't let you do something that makes no sense.
anotherwords... i++++++ is the logical eqivelant of i++

Does that help?

Sep 12 '05 #2
lovecreatesbeau ty wrote:
Hello experts,

Why can this difference between prefix increment/decrement and
postfix increment/decrement reside in built-in operators for built-in
data types? Thanks.

// test.cpp
//
// >g++ test.cpp
// test.cpp: In function `int main()':
// test.cpp:21: non-lvalue in increment
// test.cpp:22: non-lvalue in decrement
//

#include <iostream>
using std::cout;

int main()
{
int i = 0;

cout << ++++++i;
cout << ------i;
cout << i++++++; // non-lvalue in increment
cout << i------; // non-lvalue in decrement
}


Think about how you would implement a post inc/dec operator vs a pre
dec/inc operator.

struct IntThing
{
int value;

IntThing( int v = 0 )
: value( v )
{
}

// post decrement
const IntThing operator -- ()
{
int value_tmp = value;
value = value + 1; // cannot return a reference because
// the actual value would be wrong !
return IntThing( value_tmp );
}

// pre decrement
IntThing & operator -- ( int )
{
value = value + 1;
return * this; // can simply return a reference.
}

};
Sep 12 '05 #3
John Fullman wrote:
Prefix inc/dec changes that contents of the variable immediatley, which
means no additional variables would have to placed on the stack:
C++:
++++++i;
Assembly:
inc i
inc i
inc i

Postfic inc/dec delays changing the value of the variable until the end
of the line...


That's exactly the explanation that ends up confusing newbies about
postfix increment. It's not even true.

All operators have two aspects, a return value and (optionally) a side
effect. Prefix and postfix increment have the side effect of
incrementing the variable, this side effect happens immediately, not at
'the end of the line'. Where they differ is in their return value.
Prefix returns the value of the variable after it was incremented,
postfix returns the value of the variable before it was incremented.
Because prefix return the current value of the variable it can return a
reference to that variable (i.e. an lvalue). Because postfix returns the
old value of the variable it cannot do this, therefore it must return an
rvalue.

This explanation works equally well for built-in types and classes.

john
Sep 12 '05 #4
Thank Gianni. But in my demo code snippet the data type is built-in
`int'. Does standard-compliant compilers implement those prefix/postfix
increment/decrement operators also in that way actually.
Thank Fullman and Harrison. I don't consider your advice reliable. Even
in C, both ------i; and i------; are illegal. There should be something
special in C++ make the prefix one correct.

Sep 12 '05 #5
lovecreatesbeau ty wrote:
Thank Gianni. But in my demo code snippet the data type is built-in
`int'. Does standard-compliant compilers implement those prefix/postfix
increment/decrement operators also in that way actually.
Thank Fullman and Harrison. I don't consider your advice reliable. Even
in C, both ------i; and i------; are illegal. There should be something
special in C++ make the prefix one correct.


The standard explains the difference in 5.2.6 and 5.3.2. The gist of it is,
as John Harrison already explained, that both, postfix and prefix -- want
their argument to be an lvalue, but only prefix -- returns an lvalue.

Now, that does not say that ----i is legit (there might be something about
sequence points and undefined behavior), but at least the compiler is not
required to complain.
Best

Kai-Uwe Bux

Sep 12 '05 #6
lovecreatesbeau ty wrote:
Thank Gianni. But in my demo code snippet the data type is built-in
`int'. Does standard-compliant compilers implement those prefix/postfix
increment/decrement operators also in that way actually.
Standard compliant compilers have to implement the standard.


Thank Fullman and Harrison. I don't consider your advice reliable. Even
in C, both ------i; and i------; are illegal. There should be something
special in C++ make the prefix one correct.

Sep 12 '05 #7
lovecreatesbeau ty wrote:
Thank Gianni. But in my demo code snippet the data type is built-in
`int'. Does standard-compliant compilers implement those prefix/postfix
increment/decrement operators also in that way actually.

No. They do not.

Most compilers would add 3 to the memory location.
Sep 12 '05 #8
Kai-Uwe Bux wrote:
lovecreatesbeau ty wrote:

Thank Fullman and Harrison. I don't consider your advice reliable.
Even in C, both ------i; and i------; are illegal. There should be
something special in C++ make the prefix one correct.


The standard explains the difference in 5.2.6 and 5.3.2. The gist of
it is, as John Harrison already explained, that both, postfix and
prefix -- want their argument to be an lvalue, but only prefix --
returns an lvalue.

Now, that does not say that ----i is legit (there might be something
about sequence points and undefined behavior), but at least the compiler
is not required to complain.


In fact, it clearly modifies 'i' twice without a sequence point
(causing undefined behaviour).

Sep 12 '05 #9

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

Similar topics

6
2239
by: Sergey | last post by:
Hello! Could anybody be kind enough to explain this concept? Why C++ make two ops for prefix and postfix ++ operator? As I guess, it is possible to implement both cases via sole prefix increment operation. Correct me if I'm wrong, Let's see trival example:
2
3624
by: Christian Christmann | last post by:
Hi, is there a difference in terms of efficiency when using a prefix "++x;" instead of a postfix "x++;"? Thanks Chris
98
14367
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
8
3184
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
6
1430
by: LuciferLeo | last post by:
given i = 0, I know i = i + 1 and i += 1 are all correct but when I type: >>> i++ the interpreter replies: File "<stdin>", line 1 i++ ^ SyntaxError: invalid syntax
8
7826
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
2
2496
by: swapnaoe | last post by:
Hi, In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators. It said " If the increment or decrement operator is used as a prefix, the operation is performed before the function call. If the operator is used as a postfix, the operation is performed after the...
3
2029
by: subramanian100in | last post by:
Consider the code fragment: vector<intcontainer; container.insert(container.begin(), 10); int& ref = *--container.end(); From this, it looks like we can apply prefix decrement operator to container.end() - ie 'prefix --' can be applied to the iterator type
3
3798
by: news.aioe.org | last post by:
Is it possible to overload increment(++) and decrement(--) postfix and prefix operators for primitive datatypes such as int, char, short, etc. in global scope (vs as a class member function where this is possibe)? If there is a site that lists all operators that can't be overloaded (such as the member access dot operator), please post it. ...
0
7807
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8058
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7820
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8100
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6456
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5624
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2249
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.