Search code examples
c++variable-assignmentdecrement

Is the behaviour of this statement of assignment and decrementation well defined?


The following statement does not really make sense:

value = value--;

I accidentially wrote this instead of just value-- or value = value-1 . But since it worked on my machine as intended, the error was not noticed.

Now I want to know, if this statement is well defined and will have the same result on all machines. Or can this lead to different results?


Solution

  • No, this is not well-defined, even in C++17. (Assuming built-in =, not a function call). The assignment to the left side is sequenced-after the calculation of the value on the right side. But the side effects of the right-hand side (value--) are not sequenced relative to the assignment. And decrementing value is a side effect of value--.