Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

standards

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 ?

like image 857
BlueTrin Avatar asked Aug 30 '12 19:08

BlueTrin


1 Answers

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

like image 148
Dirk Holsopple Avatar answered Sep 27 '22 23:09

Dirk Holsopple