Is the array [i] = i ++ covered by the C ++ standard?

I had a person claiming that this line does not apply to the C ++ standard:

int i(1); array_of_int[i] = i++; 

The man said that he will assign 1 , but we cannot know whether he will be in array_of_int[1] or array_of_int[2] , although the visual studio and most compilers will be in array_of_int[1] .

Is he right?

+6
source share
1 answer

This behavior is undefined. Literally any behavior is legal.

The pass that prohibits this line of code is this:

Between the previous and next points in the sequence, the object must have the changed value of the stored value no more than once by evaluating the expression. In addition, the previous value should only be read to determine the stored value.

There is no sequence point between a[i] and i++ , and reading i in a[i] not intended to determine what value is stored in i on i++ .

+6
source

Source: https://habr.com/ru/post/924201/


All Articles