If you are not compiling to the C++17 Standard revision or more recent you have encountered undefined behaviour. The older C++ Standards do not specify the sequencing of
cout << c << "," << c++ << endl;
so there are no guarantees on when that c++
will occur. The only thing you can count on is the c++
term will be the initial value because ++
increments after the value is collected.
a,a
or
b,a
are valid outputs.
As of C++17 the Standard guarantees that all side effects will be resolved before proceeding to the next <<
. << c
will be resolved, not that there is much to resolve, before << ","
starts. << c++
comes even later in the chain. This means you should always see
a,a
b,b
c,c
See the notes on Undefined Behaviour at the bottom of Order of evaluation