samedi 28 mars 2015

Swap integers via XOR in single line. Is it really allowed in c++11?

I still could not clearly understand whether the expression x ^= y ^= x ^= y; valid in C++11 (as they say in this thread) or it leads to undefined behavior?


The reasons given by the link seem convincing, but clang throws a warning:



warning: unsequenced modification and access to 'x' [-Wunsequenced]



Moreover, if both versions:



x ^= y ^= x ^= y; // (1)
x = x ^ (y = y ^ (x = (x ^ y))); // (2)


considered equivalent (and well-defined in C++11), why it gives different results (first, second)?


Additionally, it should be noted that the gcc gives a warning about sequence point only on the second version of code.


Aucun commentaire:

Enregistrer un commentaire