No, the output does not depend on the compiler (modulo the bits/stdc++.h
nonsense). The order in which the three calls foo(i)
are evaluated is unspecified, but that doesn't affect the output: function calls are not interleaved, so some call will increment i
to 1, print that, and return it (as a copy), then one of the other two will assign, print, and return 2, then the last will do 3, and their return values will always sum to 6.
Note that if foo
returned int&
, the last output might be any digit 6–9, since the implementation might not choose to read i
for the addition immediately upon return. The behavior would still be defined, though, since those reads would be indeterminately sequenced with respect to any write.