c++ - Is (++i)++ legal?
- =?ISO-8859-1?Q?=22Sz=2E_Horv=E1t=22?= (13/13) Oct 09 2006 Okay, I know that this is a stupid question, but I am curious:
- Matthew (3/16) Oct 10 2006 It's a bug in DMC++
- Derek Parnell (19/40) Oct 10 2006 Why do you think this is a bug?
- Derek Parnell (8/11) Oct 10 2006 Further more, if '(++i)++' was legal then (3)++ should also be legal.
- Matthew (3/8) Oct 10 2006 No. i may (and in this case must) be an lvalue. 3 is not an lvalue.
- Matthew (17/41) Oct 10 2006 C++-98: 5.3.2;1 - "The value is the new value of the operand: it is an
- Sean Kelly (14/62) Oct 10 2006 Are you sure? From 5.1.4 (2003 revision):
- Sean Kelly (4/5) Oct 11 2006 I suppose I should clarify. By illegal I meant "undefined" -- the
- Derek Parnell (15/24) Oct 10 2006 Thanks. My memory was faulty yet again :-)
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
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
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...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 AMOkay, 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++
Oct 10 2006
On Wed, 11 Oct 2006 09:29:17 +1000, Derek Parnell wrote:On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote: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 AMIt's a bug in DMC++
Oct 10 2006
"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:No. i may (and in this case must) be an lvalue. 3 is not an lvalue.On Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote:Further more, if '(++i)++' was legal then (3)++ should also be legal.It's a bug in DMC++
Oct 10 2006
"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: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"Sz. Horvát" <szhorvat-nospamplease gmail.com> wrote in message news:egdsnm$jvm$1 digitaldaemon.com...Why do you think this is a bug?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++
Oct 10 2006
Matthew wrote:"Derek Parnell" <derek nomail.afraid.org> wrote in message news:texhpcyj3e5c.x2tpmo3z3l60.dlg 40tude.net...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? SeanOn Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote: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"Sz. Horvát" <szhorvat-nospamplease gmail.com> wrote in message news:egdsnm$jvm$1 digitaldaemon.com...Why do you think this is a bug?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++
Oct 10 2006
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
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...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 AMOn Wed, 11 Oct 2006 07:37:50 +1000, Matthew wrote:C++-98: 5.3.2;1 - "The value is the new value of the operand: it is an lvalue"It's a bug in DMC++Why do you think this is a bug?
Oct 10 2006