++ operand order driving me up the wall

Why in the world would it process from right to left ? who does math in that order, especially addition !

1
2
  int a = 1;
  cout  << a << a++ << a++ << a << endl;


I would assume the output would be

1 1 2 3

but noooooo

3 2 1 3

is the output because from right to left it does the ++ then rest LAST.

Does anyone else see this as incredibly retarded or is it just me ?

the reason this came up is I have an array called buffer and an index names i, a function called mk16 which takes two unsigned char and a mask and converts them to an unsigned int.

i = 16; // this is the index at where my length variable exists, after the length is the X number of bytes.

length = mk16(buffer[i++], buffer[i++], 0x0FFF);
for(int a = 0; a < length; a++)
cout << buffer[i++];

nice right ? simple clean effective, doesnt work cause of how the ++ are sorted right to left.

That is an interesting bug, I haven't tried the ++ operand multiple times on the same line before, weird logic there.
Hmm, and it won't even take += as an operand.
I wonder if you can overload your operators to make this work properly. I haven't tried overloading so I don't know if that's possible here...
Using the post-increment operator multiple times on the same variable in the same expression is undefined behaviour.

Also, with regards to values passed to functions
such as mk16(buffer[i++], buffer[i++], 0x0FFF); I believe C++
does not dictate the order in which the parameter values are calaculated.
(I'll look up the c++ standard later if only to recheck)
So parameter value calulations should not depend on each other.
Topic archived. No new replies allowed.