473,664 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is this expression illegal?

Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??

Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?

-
Chris
Jul 22 '05 #1
12 1849
Chris Yates wrote:
Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??

Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?


Nope. Post-increment returns an r-value. When it increments the actual
object is unspecified.

V
Jul 22 '05 #2


Chris Yates wrote:
Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??

Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?


if i is say an integer, --i (or ++i) does something before accessing the
variable... whereas i++(or i--) does something after accessing the variable. In
the former case the variable changes before you get its value so everything is
kosher, but in the latter case the variable may change value between the time it
is fetched and the time you use it. (I don't know if it HAS to change
immediately or it can wait till the next sequence point, but there are common
cases where compilers do try to adhere to changing it immediately to make users
happy).

David
Jul 22 '05 #3
On Wed, 03 Nov 2004 22:52:50 GMT, "Chris Yates" <us****@iyates. com>
wrote:
Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??

Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?


I am not sure, but I believe both expressions lead to undefined
behaviour, RE: the C++ standard, due to the lack of a sequence point
between increment and decrement.

There are some threads on comp.std.c++ about this. Please try to look
at these, as well as the standard itself, for more of an in-depth
explanation. Unfortunately, I don't have the subject lines at hand...

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #4
Chris Yates wrote:
Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??
Both are illegal, but for different reasons. The first one is
ill-formed, the second one causes undefined behavior.
Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?


The C++ language doesn't specify what happens "before" and what happens
"after" in this case. The argument of the post-increment ('i') might get
incremented immediately, it might get incremented "after the
expression", it might get incremented "before the expression" - no one
knows and no one cares, since in any case the result must be the
original value of 'i'. Hence the potential need to take it out of 'i'
and store it "on the side", as an rvalue.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #5
Chris Yates wrote:
Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??

Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the
l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?


Think about it. How would the operator return something, and _after_ that
change the value? After it returned, it cannot do anything anymore.

Jul 22 '05 #6
Victor Bazarov <v.********@com Acast.net> wrote in message news:<iC******* *********@newsr ead1.dllstx09.u s.to.verio.net> ...
Chris Yates wrote:
Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??

Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?


Nope. Post-increment returns an r-value. When it increments the actual
object is unspecified.


Post increment, actually, returns a copy of the original object, which
is created before the original object is modified. Therefore
(offtopic) the postfix operator is, actually, slower.

Standard 5.2.6.

d
Jul 22 '05 #7
In message <10************ *@news.supernew s.com>, Andrey Tarasevich
<an************ **@hotmail.com> writes
Chris Yates wrote:
Why is this illegal:

--(i++) // Error: -- needs an l-value

and this is not:

(--i)++

??
Both are illegal, but for different reasons. The first one is
ill-formed, the second one causes undefined behavior.
Why is i++ not an l-value but --i is? Pre incrementor adds 1 to the l-value
and returns the l-value, so that explains that. However, I thought i++
would return the l-value and the increment it after the expression?


The C++ language doesn't specify what happens "before" and what happens
"after" in this case.


More to the point, nothing in the above says that i has to be of a
built-in type. If the operator is user-defined, there _is_ no "before"
and "after". Everything the operator does must take place _during_ the
call to it. The only way you can get the right semantics for
post-increment within a single function call is to take a copy, then
increment i, then return the copy by value, hence an rvalue.
The argument of the post-increment ('i') might get
incremented immediately, it might get incremented "after the
expression", it might get incremented "before the expression" - no one
knows and no one cares, since in any case the result must be the
original value of 'i'. Hence the potential need to take it out of 'i'
and store it "on the side", as an rvalue.


--
Richard Herring
Jul 22 '05 #8
Andre Dajd wrote:
...
Nope. Post-increment returns an r-value. When it increments the actual
object is unspecified.
Post increment, actually, returns a copy of the original object, which
is created before the original object is modified.


Incorrect. The notion of "returning a copy" (or simply "returning" ) the
way you use it is not applicable to built-in increment operators.
Therefore (offtopic) the postfix operator is, actually, slower.
True for user-defined operators. Completely untrue for built-in ones.
And we are talking about the latter here.
Standard 5.2.6.


I don't see anything you said in 5.2.6.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #9
Andrey Tarasevich <an************ **@hotmail.com> wrote in message news:<10******* ******@news.sup ernews.com>...
Andre Dajd wrote:
...
Nope. Post-increment returns an r-value. When it increments the actual
object is unspecified.
Post increment, actually, returns a copy of the original object, which
is created before the original object is modified.


Incorrect. The notion of "returning a copy" (or simply "returning" ) the
way you use it is not applicable to built-in increment operators.


See first [Note] in the citation below and note world "copy" there.
Fine, I withdraw word "return", as using it in the context requires
abstract thinking beyond repeating C++ Standard's deficiencies.
Mathematically (=abstractly) every operation (mapping) "returns"
something (has image).
Therefore (offtopic) the postfix operator is, actually, slower.


True for user-defined operators. Completely untrue for built-in ones.
And we are talking about the latter here.


Original question did not imply that, neither did any post in this
subthread. Mind your perceptions.
Standard 5.2.6.


I don't see anything you said in 5.2.6.


[expr.post.incr] 5.2.6 Increment and decrement

The value obtained by applying a postfix ++ is the value that the
operand had before applying the operator.
[Note: the value obtained is a copy of the original value ] The
operand shall be a modifiable lvalue. The
type of the operand shall be an arithmetic type or a pointer to a
complete object type. After the result is
noted, the value of the object is modified by adding 1 to it, unless
the object is of type bool, in which case
it is set to true. [Note: this use is deprecated, see annex D. ] The
result is an rvalue. The type of the
result is the cvunqualified
version of the type of the operand. See also 5.7 and 5.17.
Jul 22 '05 #10

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

Similar topics

4
2022
by: Rex_chaos | last post by:
Hi all, As some book tells, I try the following example of expression template. template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
12
2170
by: Ali | last post by:
I have the following web page with a script in it. <html> <head> <title>Make Your Own Objects test</title> <script>
3
5679
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't know how to understand it, How to evaluate the expression as a void expression? explicit conversion the expression? but, befor the sentence ,"implicit or explicit conversions (except to void) shall not
24
1783
by: s.subbarayan | last post by:
Dear all, According to standards is this valid: char TmpPtrWriteBuffer; void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to void* though the reverse is true.But they said this is valid. I just can't agree with them with out a valid explaination.Can any C standard experts clarify me this?
9
1976
by: ais523 | last post by:
I have some code, which I know will not work (I haven't included <math.h>, so sqrt will be assumed to return an int). What I want to know is, is an ANSI-compliant implementation required to produce an executable as output (even if the actual output is undefined)? #include <stdio.h> /*#include <math.h>*/ int main() {
28
2118
by: ensemble | last post by:
I'm trying to utilized a more object-oriented approach to managing window events in javascript. Thus, I am creating a "controller" object to handle events and interact with the server. However, I apparently don't fully understand what "this" refers to. In the code below I was expecting that when the button was clicked (handleDeleteButtonClicked function) that "this" would be pointing at a ViewController object. Instead it was the Button...
11
2955
by: nevergone | last post by:
Hello Everybody In <<Modern C++ Design>Compile-Time Assertions there is : template <boolstruct CompileTimeChecker { CompileTimeChecker(...); }; template <struct CompileTimeChecker<false{ };
7
1300
by: Lippman.Gan | last post by:
Sorry for that I can not clarify the type of issue in topic. Look the code below: template <class T> class A { public: class a {
18
1678
by: Richard Eich | last post by:
I need a clue. I'm getting an "invalid lvalue in increment" from gcc 4.1.2 on this line... sum += *((unsigned short *)iphp)++ ; ....but I don't see what the problem is. gcc 3.4.3 isn't bother by it, either. Machine specifics:
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8636
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.