Yes, it is safe (at least in this case). You use only two "operators", the primary expression (something)
and the binary something +/- something
(additive).
Section 1.9 Program execution
(from C ++ 0x N3092) reads:
Operators can be rearranged in accordance with normal mathematical rules only in cases where the operators are indeed associative or commutative.
In terms of grouping 5.1 Primary expressions
indicates:
An expression in parentheses is the main expression, the type and value of which are identical to the type of the enclosed expression ... An expression enclosed in parentheses can be used in exactly the same contexts as those in which a closed expression can be used, and with the same value , unless otherwise specified.
I believe that the use of the word βidenticalβ in this citation requires a coordinated implementation to ensure that it is executed in the indicated order if a different order cannot give accurate results.
And for addition and subtraction, section 5.7 Additive operators
has:
Additive operators + and - group from left to right.
Thus, the standard dictates the results. If the compiler can make sure that the same results can be obtained with different ordering of operations, then he can reorder them. But whether this happens or not, you cannot distinguish the difference.
source share