Consider following code:
#include <iostream>
int main()
{
int i = 0;
std::cout << ++i << ' ' << --i << std::endl;
}
In his "C++17 The Complete Guide" book Nicolai Josuttis writes that before C++17 this particular example might produce unpredictable results. Both 1 0 and 0 1 might be produced but what's more 0 0 is also a possible output. I don't get why the third sequence should be considered as possible. Either ++i or --i should be evaluated before the second value is evaluated which by definition cannot produce two zeros, shouldn't it?
Before C++17, the
0 0outcome was possible roughly like this.++iis evaluated.inow stores1. The result of this expression (which, importantly, is an l-value reference toi, not the r-value1) is set aside as an argument to the<<operator.--iis evaluated.inow stores0. The result of this expression (which, importantly, is an l-value reference toi, not the r-value0) is set aside as an argument to the<<operator.<<operators are evaluated, where both arguments are references toiwhich has value0.