I have a legacy code performing double buffering with this instruction:
bufferIndex = ++bufferIndex & 1;
clang warns about unsequenced modification order of bufferIndex
warning: multiple unsequenced modifications to 'bufferIndex' [-Wunsequenced]
In the doubt, I would avoid such construct, especially since knowing that the bufferIndex has been appropriately initialized to either 0 or 1, this flip/flop could have been written more simply:
bufferIndex ^= 1;
Even without a doubt, I would avoid such pre-increment construct so that I don't cause confusion in my rewiewers mind, and so that I don't generate brainstorming warnings uselessly.
But that's not my point. My point is that I want to understand if unsequenced modification is possibly true here for my own culture.
Is the evaluation order really undefined in this case, or is the warning a bit abusive?
Note: it's not the same case as Unsequenced modification warning where the variable clearly appear twice in an unsequenced manner, nor in the other possible duplicates suggested by SO (unless I overlooked).
It's not at all obvious that this applies to this case of assignment operator = to my understanding, since expression has to be evaluated BEFORE being assigned to, and since the sole side effect is pre-increment and will necessarily happen sometime during evaluation and before assignment.