The C++17 standard revised the definitions of the order of operations for the C++ language by a rule stating, to the effect:
In every simple assignment expression E1=E2 and every compound assignment expression E1@=E2, every value computation and side-effect of E2 is sequenced before every value computation and side effect of E1
However, when compiling the following code in GCC 8.1 with -std=c++17
and -Wall
int v[] { 0,1,2,3,4,5,6,7 };
int *p0 = &v[0];
*p0++ = *p0 + 1;
cout << "v[0]: " << v[0] << endl;
I get the following warning:
main.cpp:266:8: warning: operation on 'p0' may be undefined [-Wsequence-point]
*p0++ = *p0 + 1;
~~^~
The output is:
v[0]: 1
And the question: is the warning erroneous?
*p0 + 1
will be evaluated before anything on the left. Since the++
is postfix, whether it gets evaluated or not does not effect the assignment tov[0]
. The real question is whetherp0
now points tov[1]
. – Cattleya-Wall
– Arsphenamine-Wall
– Isborne