In the following C++ code is there a difference between the values of x
and y
at the end?
const int LoopLength = 100;
unsigned short x = 0;
for (int i = 0; i < LoopLength; i++)
{
x = ++x % 2;
cout << x;
}
cout << endl;
unsigned short y = 0;
for (int i = 0; i < LoopLength; i++)
{
++y = y % 2;
cout << y;
}
cout << endl << (x == y) << endl;
Coverity (static-analysis tool) claims that the order in which the side-effects take place is undefined with a line like x = ++x % 2;
. I'm unsure if I should worry about it.