I just ran into a piece of code that not only compiles, but gives the expected result (where x is an integer value):
int y = (int)(0.5 * x * x + + + 0.6 * x + 1.2);
It took me a while to figure out what happens and I must say it was an interesting operator problem. Without compiling the program, what are the results of the following operations and why?
int a = 1;
int b = 2;
int z = a + + + b;
int z1 = a + - + b;
int z2 = a + - - b;
int z3 = a - - + b;
int z4 = a - - - b;
int z5 = a +- b;
I still have one question, though: does the standard give such results or is it compiler specific?
Explanation: Because the + and - operators have spaces between them, the "+ + +" sequence is not compiled as "++ +", but as unary operators on the right member. So
int y = (int)(0.5 * x * x + + + 0.6 * x + 1.2);
actually gives:
int y = (int)(0.5 * x * x + 0.6 * x + 1.2);
which was the expected result.
So,
z = a + + + b = a + + (+b) = a + (+b) = a + b = 3;
z1 = a + - + b = a + - (+b) = a + (-b) = a - b = -1;
z2 = a + - - b = a + - (-b) = a + (+b) = a + b = 3;
z3 = a - - + b = a - - (+b) = a - (-b) = a + b = 3;
z4 = a - - - b = a - - (-b) = a - (+b) = a - b = -1;
z5 = a +- b = a + (-b) = a - b = -1;