Why does the following given expression invoke undefined behavior?
int i = 5;
i = (i,i++,i) + 1
My question is influenced by Als' question here
It isn't undefined.
Answered here for C, Sequence points and partial order
I think the same applies in C++ (and here's my response before I saw that link):
The comma operator introduces a sequence point (and constrains to some extent the order in which the expression must be evaluated - left before right), so:
i
are separated by a sequence point (the second comma).i
in i++
is separated from everything else by sequence points.i
by =
is not separated from the last occurrence of i
in the expression, but that's OK because we're allowed to access i
and modify it without an intervening sequence point, provided that the access is "to determine the value to be stored" (5/4).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With