www.digitalmars.com         C & C++   DMDScript  

c++ - Is (++i)++ legal?

reply =?ISO-8859-1?Q?=22Sz=2E_Horv=E1t=22?= writes:
Okay, I know that this is a stupid question, but I am curious:

Should the following be legal?

#include <iostream>

using namespace std;

int main() {
     int i = 0;
     (++i)++;            // <-- the question is about this
     cout << i << endl;
     return 0;
}

It does compile with gcc but not with dmc 8.49. I thought that (++i) 
should be an lvalue. If it is legal then why does the posfix operator 
have a higher precedence than the prefix operator?
Oct 09 2006
parent reply "Matthew" <matthew hat.stlsoft.dot.org> writes:
It's a bug in DMC++

"Sz. Horvát" <szhorvat-nospamplease gmail.com> wrote in message
news:egdsnm$jvm$1 digitaldaemon.com...
 Okay, I know that this is a stupid question, but I am curious:

 Should the following be legal?

 #include <iostream>

 using namespace std;

 int main() {
      int i = 0;
      (++i)++;            // <-- the question is about this
      cout << i << endl;
      return 0;
 }

 It does compile with gcc but not with dmc 8.49. I thought that (++i)
 should be an lvalue. If it is legal then why does the posfix operator
 have a higher precedence than the prefix operator?
Oct 10 2006
parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote: 
 "Sz. Horvát" <szhorvat-nospamplease gmail.com> wrote in message
 news:egdsnm$jvm$1 digitaldaemon.com...
 Okay, I know that this is a stupid question, but I am curious:

 Should the following be legal?

 #include <iostream>

 using namespace std;

 int main() {
      int i = 0;
      (++i)++;            // <-- the question is about this
      cout << i << endl;
      return 0;
 }

 It does compile with gcc but not with dmc 8.49. I thought that (++i)
 should be an lvalue. If it is legal then why does the posfix operator
 have a higher precedence than the prefix operator?
It's a bug in DMC++
Why do you think this is a bug? Is '++' identical in meaning to '+='? And if so, then an assignment is implied in '++' and thus it needs a memory address. (++i) is an expression that adds one to 'i' and evaluates to the resulting value and not a memory address. If '(++i)++' was valid, then '(j=i)++' should also be valid. And it would be if '++' was not identical to '+='. I'm pretty sure that '++' means 'add one to the variable operand and return the result' and it does *not* mean 'if the operand is a variable then add one to it and return the result otherwise just add one to the operand's value and return the result'. Where is the exact definition of '++' as I'm just going off memory here so it could be highly suspect ;-) -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 11/10/2006 9:21:53 AM
Oct 10 2006
next sibling parent reply Derek Parnell <derek nomail.afraid.org> writes:
On Wed, 11 Oct 2006 09:29:17 +1000, Derek Parnell wrote:

 On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote: 
 It's a bug in DMC++
Further more, if '(++i)++' was legal then (3)++ should also be legal. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 11/10/2006 10:02:33 AM
Oct 10 2006
parent "Matthew" <matthew hat.stlsoft.dot.org> writes:
"Derek Parnell" <derek nomail.afraid.org> wrote in message
news:pykh8vjb00oz$.1vipkpdvx9igh$.dlg 40tude.net...
 On Wed, 11 Oct 2006 09:29:17 +1000, Derek Parnell wrote:

 On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote:
 It's a bug in DMC++
Further more, if '(++i)++' was legal then (3)++ should also be legal.
No. i may (and in this case must) be an lvalue. 3 is not an lvalue.
Oct 10 2006
prev sibling parent reply "Matthew" <matthew hat.stlsoft.dot.org> writes:
"Derek Parnell" <derek nomail.afraid.org> wrote in message
news:texhpcyj3e5c.x2tpmo3z3l60.dlg 40tude.net...
 On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote:
 "Sz. Horvát" <szhorvat-nospamplease gmail.com> wrote in message
 news:egdsnm$jvm$1 digitaldaemon.com...
 Okay, I know that this is a stupid question, but I am curious:

 Should the following be legal?

 #include <iostream>

 using namespace std;

 int main() {
      int i = 0;
      (++i)++;            // <-- the question is about this
      cout << i << endl;
      return 0;
 }

 It does compile with gcc but not with dmc 8.49. I thought that (++i)
 should be an lvalue. If it is legal then why does the posfix operator
 have a higher precedence than the prefix operator?
It's a bug in DMC++
Why do you think this is a bug?
C++-98: 5.3.2;1 - "The value is the new value of the operand: it is an lvalue" Or, from common experience, when one implements operator ++, one does the following: class X { public: X &operator ++(); // prefix. Return ref (to self) X operator ++(int); // postfix. Return copy }; Thus X x; (++(++(++(++(++x))))); // legal and even ++++++++++x; // legal
Oct 10 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
Matthew wrote:
 "Derek Parnell" <derek nomail.afraid.org> wrote in message
 news:texhpcyj3e5c.x2tpmo3z3l60.dlg 40tude.net...
 On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote:
 "Sz. Horvát" <szhorvat-nospamplease gmail.com> wrote in message
 news:egdsnm$jvm$1 digitaldaemon.com...
 Okay, I know that this is a stupid question, but I am curious:

 Should the following be legal?

 #include <iostream>

 using namespace std;

 int main() {
      int i = 0;
      (++i)++;            // <-- the question is about this
      cout << i << endl;
      return 0;
 }

 It does compile with gcc but not with dmc 8.49. I thought that (++i)
 should be an lvalue. If it is legal then why does the posfix operator
 have a higher precedence than the prefix operator?
It's a bug in DMC++
Why do you think this is a bug?
C++-98: 5.3.2;1 - "The value is the new value of the operand: it is an lvalue" Or, from common experience, when one implements operator ++, one does the following: class X { public: X &operator ++(); // prefix. Return ref (to self) X operator ++(int); // postfix. Return copy }; Thus X x; (++(++(++(++(++x))))); // legal and even ++++++++++x; // legal
Are you sure? From 5.1.4 (2003 revision): 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. i = ++i + 1; // the behavior is unspecified So by my reading, modifying x twice in the same expression--no matter how it's done--has an undefined result. Wouldn't this make your examples above illegal? Sean
Oct 10 2006
parent Sean Kelly <sean f4.ca> writes:
Sean Kelly wrote:
 Wouldn't this make your examples above illegal?
I suppose I should clarify. By illegal I meant "undefined" -- the compiler obviously isn't required to flag such code as an error. Sean
Oct 11 2006
prev sibling parent Derek Parnell <derek nomail.afraid.org> writes:
On Wed, 11 Oct 2006 10:12:00 +1000, Matthew wrote:

 "Derek Parnell" <derek nomail.afraid.org> wrote in message
 news:texhpcyj3e5c.x2tpmo3z3l60.dlg 40tude.net...
 On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote:
 It's a bug in DMC++
Why do you think this is a bug?
C++-98: 5.3.2;1 - "The value is the new value of the operand: it is an lvalue"
Thanks. My memory was faulty yet again :-) Is there any practical usage for this idiom? It seems to me that if one increments a variable that is immediately used as a lvalue, the effect of the increment is lost. Unless there are side-effects for an Object, which sounds like a dubious practice anyhow. (++i) = j; // why bother with the increment? And something like '(++i)++' is just got to be an exercise in obfuscation, no? -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 11/10/2006 11:01:00 AM
Oct 10 2006