c++ function invoking order in f() + g() [duplicate]

  • A+

This question already has an answer here:

If I do like this

f() + g(); 

or like this

h(f() + g()); 

Is it guaranteed in C++ that f() is called first?


C++ does not guarantee the order of evaluation of the operands of the + operator.

Either f() or g() may be called first.

This is specified by section 1.9p15 of the C++ standard:

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. — end note ] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: