I vaguely remember reading somewhere that it is undefined behaviour if multiple operands in a compound expression modify the same object.
I believe an example of this UB is shown in the code below however I've compiled on g++, clang++ and visual studio and all of them print out the same values and can't seem to produce unpredictable values in different compilers.
#include <iostream>
int a( int& lhs ) { lhs -= 4; return lhs; }
int b( int& lhs ) { lhs *= 7; return lhs; }
int c( int& lhs ) { lhs += 1; return lhs; }
int d( int& lhs ) { lhs += 2; return lhs; }
int e( int& lhs ) { lhs *= 3; return lhs; }
int main( int argc, char **argv )
{
int i = 100;
int j = ( b( i ) + c( i ) ) * e( i ) / a( i ) * d( i );
std::cout << i << ", " << j << std::endl;
return 0;
}
Is this behaviour undefined or have I somehow conjured up a description of supposed UB that is not actually undefined?
I would be grateful if someone could post an example of this UB and maybe even point me to where in the C++ standard that it says it is UB.