MiddleCodeOccasionalNot answered yet
What does std::cout << i++ << i++ << i++ print?
Predict the output across C++ standards.
int i = 0;
std::cout << i++ << " " << i++ << " " << i++;
Determine the output and explain.
Through C++14 the operand evaluation order of << was unspecified, so it could print 2 1 0 or 0 1 2. C++17 made << operands evaluate left to right, so it now reliably prints 0 1 2. General function-argument order is still unspecified — avoid such side effects.
- ✗Assuming chained
<<always evaluated left to right before C++17 - ✗Calling it undefined behavior — it is unspecified, since each
i++is sequenced across a separate<<call - ✗Generalizing the C++17 fix to all function arguments
- →What is the difference between unspecified evaluation order and undefined behavior here?
- →Does the C++17 left-to-right rule apply to ordinary function arguments?