Fun with Operator Shortcuts

Started by
15 comments, last by Rob Loach 20 years, 10 months ago
To my knowledge:

int o = 2;o--; 

is equivilent to:
int o = 2;--o; 

But, in the first one, o is set to o - 1 after the statement is fully ran by the compiler. In the second one, o is set to o -1 before the statement is ran by the compiler.


Rob Loach
Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?"
- The Boondock Saints
Rob Loach [Website] [Projects] [Contact]
Advertisement
Yes those two have the same end result, but it behaves differently if both expressions o-- and --o appear as subexpressions in a larger expression, for instance in your example "--o - o--". Which one gets evaluated first? The C++ standard simply doesn''t define that.

To simplify things a bit, it''s much like this expression:
o = f() - g();
Which function gets called first, f or g? According to the C++ standard it depends on your compiler.

What if f modifies the value of o (via a pointer for argument''s sake) and g uses the value of o (also via a pointer) when calculating its return value? What happens? Does f get called first, modify o, and then g uses the modified value? Or does g get called first and use the original value, then f is called and modifies it?

Anyway it''s getting late, I got work tomorrow, and I''m getting kind of sick of trying to convince you guys.
a-b, is computed from left to right whether a is evaluated before b is undefined.
I think it''s a non-issue anyway. If you write code like that you''ll probably be sacked in a couple of days!

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
I hope nobody would really write code like that.

Either way it''s been fun and useful. Now I know that operands may not be evaluated in the order I assume. Thanks.
quote:Original post by siaspete
I think it''s a non-issue anyway. If you write code like that you''ll probably be sacked in a couple of days!

I was just going to say that. You pull a stunt like this one in our company, and you''ll be looking for a new job the next day. I guess that most other software companies will handle this in a similar harsh way.

quote:
What''s so undefined about that? Most compilers will do it like this- the fact that they don''t have to isn''t very practical. Unless you are participating in the GNU project, you are probably using one or two compilers on one or two platforms, and you don''t need to worry about stuff like the standard all the time.

You would have fun in the industry (not). Relying on undefined behaviour is one of the capital sins in software engineering. As someone mentioned above, imagine a 1m+ LOC project that will suddendly break on a new/different compiler setup. The costs of fixing idiotic abominations as the "--o - o--" one can cost millions. It could even ruin the company.
quote:Original post by Rob Loach
To my knowledge:

int o = 2;o--;  

is equivilent to:
int o = 2;--o;  



To expand a bit on what Dobb said about this, consider this example:

int o=2;int n;n=--o;//vsn=o--; 


For the first one,

n=--o; //is the same as sayingo=o-1;n=o; 


The second one,

n=o--;//is the same as sayingn=o;o=o-1; 

--{You fight like a dairy farmer!}

This topic is closed to new replies.

Advertisement