samedi 19 août 2017

Is there any difference with undefined behaviour between iterator and scalar object?

The topic about evaluation order says that following code leads to undefined behavior until C++17:

a[i] = i++;

This happens due to unspecified order while evaluating left and right parts of the assignment expression.

C++14 standard 1.9/15 says:

If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, and they are not potentially concurrent (1.10), the behavior is undefined.

But what if we use std::vector and its iterator object instead of scalar object i?

std::vector<int> v = {1, 2};
auto it = v.begin();
*it = *it++;   // UB?

Is there undefined behaviour (until c++17) or not?

Aucun commentaire:

Enregistrer un commentaire