Search code examples
c++cc++11expressionoperator-precedence

order of evaluation of operands


In the expression a + b, is a guaranteed to be evaluated before b, or is the order of evaluation unspecified? I think it is the latter, but I struggle to find a definite answer in the standard.

Since I don't know whether C handles this different from C++, or if evaluation order rules were simplified in C++11, I'm gonna tag the question as all three.


Solution

  • In C++, for user-defined types a + b is a function call, and the standard says:

    §5.2.2.8 - [...] The order of evaluation of function arguments is unspecified. [...]

    For normal operators, the standard says:

    §5.4 - Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. [...]

    These haven't been changed for C++11. However, the wording changes in the second one to say that the order is "unsequenced" rather than unspecified, but it is essentially the same.

    I don't have a copy of the C standard, but I imagine that it is the same there as well.