I had a person claiming that this line is not covered by the C++ standard:
int i(1);
array_of_int[i] = i++;
The person said that it will assign 1
but we cannot know whether it will be in array_of_int[1]
or array_of_int[2]
although visual studio and most of compilers will be in array_of_int[1]
.
Is he correct ?
This is undefined behavior. Literally any behavior is legal.
The passage that forbids that line of code is this:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored
There is no sequence point between a[i]
and i++
and the read to i
in a[i]
is not for the purpose of determining what value is stored in i
by i++
.
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