JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
compiler optimization and associativity of operators | Reply
In the last rumor its mentioned
"The other is that when optimizing your code the compiler is allowed to rearrange the order in which operations are computed ...
As an example, the expression x + y - z may once be evaluated as x + (y - z) and the other time as (x + y) - z"

Isnt this against what is defined by associativity of operators. My understanding is that, as + and - are operators of same precedence and left to right associativity the expression should always be evaluated as
(x +y) - z;
Re: compiler optimization and associativity of operators (response to post by gautam2roy) | Reply
I don't understand. Does it make any diference?
Re: compiler optimization and associativity of operators (response to post by gautam2roy) | Reply
C#, for instance, guarantees the order of computation. operator precedence:
Except for the assignment operators, all binary operators are left-associative, meaning that operations are performed from left to right. For example, x + y + z is evaluated as (x + y) + z.

However, I'm not sure if c++ makes the same guarantee.
Re: compiler optimization and associativity of operators (response to post by sjelkjd) | Reply
On machines where overflows are reversible and do not cause exceptions, implementations are free to change the order of expression evaluation for built-in types taking advantage of associativity or commutativity (section 1.9.15)
Re: compiler optimization and associativity of operators (response to post by Krzysan) | Reply
thanks for that info!!
RSS