kyku
kyku

Reputation: 6042

Order of evaluation in initialization

Does the following initializations of valueA and valueB entail undefined behavior?

int array[2] = {1,2};
int index = 0;
int valueA = array[index++], valueB = array[index++];

Is there any change in this between c++ 98 and c++ 11?

Upvotes: 2

Views: 216

Answers (2)

RolandXu
RolandXu

Reputation: 3678

not undefined behavior. the comma is sequence point.

Upvotes: 0

Mat
Mat

Reputation: 206679

The behavior is well-defined. From C++11 draft n3290 §8 Declarators:

Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.

So your code is equivalent to:

...
int valueA = array[index++];
int valueB = array[index++];

I don't have a C++98 standard, but the same wording is present in ISO/IEC 14882:2003 ("C++03").

Upvotes: 5

Related Questions