c++ - Does the placement of a pre-increment operator make a difference here? -


in following c++ code there difference between values of x , y @ end?

const int looplength = 100; unsigned short x = 0; (int = 0; < looplength; i++) {     x = ++x % 2;     cout << x; }  cout << endl;  unsigned short y = 0; (int = 0; < looplength; i++) {     ++y = y % 2;     cout << y; }  cout << endl << (x == y) << endl; 

coverity (static-analysis tool) claims order in side-effects take place undefined line x = ++x % 2;. i'm unsure if should worry it.

both forms totally undefined prior c++11 because both write same memory location without intervening sequence point.

according linked question so why = ++i + 1 well-defined in c++11? first form legal in c++11, due more restricted sequencing of writes.

i believe second case not defined order of evaluation of operands = not specified.

luckily these questions can avoided never ever writing code anywhere near resembles this. future code maintainers thank , send gifts future.


Comments