Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ sequence points and changes to evaluation order in C++17 [duplicate]

I am a bit confused about the way the changes to C++17 affect the rule that you should not modify one variable 2 times between sequence points.

For example is this specified, unspecified or undefined in C++17?

void func(int x, int y);
int x=47;
func(x++,x++);

My guess would be UB.

edit: extra datapoint: clang HEAD in c++2a mode gives

prog.cc:8:11: warning: multiple unsequenced modifications to 'x' [-Wunsequenced]

edit2: same with gcc HEAD in C++2a mode

prog.cc:8:15: warning: operation on 'x' may be undefined [-Wsequence-point]

edit3: with preincrement gcc and clang give different results

like image 565
NoSenseEtAl Avatar asked Feb 03 '26 19:02

NoSenseEtAl


1 Answers

The example in your question

int x = 0;
f(x++, x++);

Is now unspecified behavior, rather than undefined behavior. That means that it’s valid code, but what actually happens may differ between compilers.

like image 125
Alecto Irene Perez Avatar answered Feb 06 '26 10:02

Alecto Irene Perez