BlueTrin
BlueTrin

Reputation: 10093

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

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 ?

Upvotes: 6

Views: 694

Answers (1)

Dirk Holsopple
Dirk Holsopple

Reputation: 8831

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++.

Upvotes: 6

Related Questions