x^=y^=x^=y;
is a tricky/amusing implementation of the XOR swap algorithm in C and C++. It parses as x^=(y^=(x^=y));
and uses the fact that assignment operators return the assigned value. But is it correct? The GCC 10.3.0 C compiler gives me the warning operation on ‘x’ may be undefined [-Wsequence-point]
and clang 12.0.0 warning: unsequenced modification and access to 'x' [-Wunsequenced]
. Compiling as C++, clang continues to warn the same way, and GCC stops. So is this code correct in either language? It looks rather sequenced to me, but maybe it's illegal to modify a variable two times in the same statement?
As pointed out in this answer, clang++ -std=c++17
does not give the warning. With -std=c++11
the situation is as described above. So maybe my question should be further broken down into C/C++11/C++17.