Quote:Original post by Sharlin
That doesn't "prove" anything, except perhaps the behaviour of one *particular* compiler version with a *particular* set of parameters.
Plz find me a compiler that does not yield this value.
Quote:Original post by Sharlin
That doesn't "prove" anything, except perhaps the behaviour of one *particular* compiler version with a *particular* set of parameters.
Quote:Original post by Suroot
Plz find me a compiler that does not yield this value.
Quote:ToohrVykQuote:Original post by greenhybrid
The compiler is very well defined by the means of operator precedence, that is operator priority as well as whether the sub-statements are evaluated left-to-right or right-to-left.
Operator precedence (or associativity) has nothing to do with evaluation order—precedence only dictates how the abstract syntax should be built, it does not impose any sense of priority or timing over the various branches of that syntax tree at evaluation time.
In particular, in an expression such as a + b, a may be evaluated before or after b depending on compile-time and even runtime considerations—the standard is pretty clear on allowing compiler writers this freedom. See this FAQ entry, for instance.Quote:Since add's go left-to-right, at first the left term (a+=2) is evaluated, and then the term on the right, (a*=2).
I'd be curious to know where you got this information (the "add's go left-to-right" part) from in the first place. Are you sure you're not confusing this with associativity? Because adds are, indeed, left-to-right associative, but associativity has no bearing here (it only plays a role when you're adding more than three subexpressions together).
Quote:
Plz find me a compiler that does not yield this value.
Quote:
At least, what I am using daily, the following fragment works fine: " (0!=(x=foo())) && x*=3; " (useless as standalone, but don't have a better example right now; btw, that style is commonly used in PHP scripts like " $value=exec() || showerror($value),die();" ).
Quote:
I will surf the net, maybe I find some more info on that (maybe I should send mail to the gcc-lists ;) )
Quote:Original post by greenhybrid
But then it would be really unintuitive if the [++*/<<>>&|...]= operators wouldn't do their jobs where there are supposed to do; every operator is like an atomic micro-program in my eyes.
Quote:At least, what I am using daily, the following fragment works fine: " (0!=(x=foo())) && x*=3; "
Quote:Original post by ToohrVykQuote:Original post by greenhybrid
But then it would be really unintuitive if the [++*/<<>>&|...]= operators wouldn't do their jobs where there are supposed to do; every operator is like an atomic micro-program in my eyes.
Well, they're not supposed to be atomic micro-programs ;)
Quote:Quote:At least, what I am using daily, the following fragment works fine: " (0!=(x=foo())) && x*=3; "
This is fine, because it only modifies x once per sequence point (yes, && acts as a sequence point).
Quote:Original post by ToohrVykQuote:Original post by greenhybrid
The compiler is very well defined by the means of operator precedence, that is operator priority as well as whether the sub-statements are evaluated left-to-right or right-to-left.
Operator precedence (or associativity) has nothing to do with evaluation order—precedence only dictates how the abstract syntax should be built, it does not impose any sense of priority or timing over the various branches of that syntax tree at evaluation time.
#include <iostream>struct foo { foo( int a ) { std::cout << a << " "; } friend foo operator+( const foo&, const foo& ) { return foo(4); }};int main() { foo(1)+foo(2)+foo(3); std::cout << std::endl;}
On WinXP + Visual Studio 2005, the result is: 3 2 1 4 4On iBook + GNU C++ Compiler 4, the result is: 1 2 4 3 4
Quote:Original post by greenhybrid
But then it would be really unintuitive if...
std::cout << x << y << z;
Quote:Original post by MaulingMonkeyQuote:Original post by greenhybrid
But then it would be really unintuitive if...
It seems your mistake was in assuming C was intuitive.
For the most part, it is the exact opposite.
Quote:Original post by greenhybrid
To make it short: To the day of this very thread, I allways assumed the operators do what they're supposed to do, 'executed' with respect to priority and associativity.