main.cpp
const int& f(int& i ) { return (++i);}
int main(){
int i = 10;
int a = i++ + i++; //undefined behavior
int b = f(i) + f(i); //but this is not
}
compile
$ g++ main.cpp -Wsequence-point
statement int a = i++ + i++; is undefined behaviour.
statement int b = f(i) + f(i); is not undefined .
why?
>Solution :
statement
int b = f(i) + f(i);is not undefined . why?
No, the second statement will result in unspecified behavior. You can confirm this here. As you’ll see in the above linked demo, gcc gives the output as 23 while msvc gives 24 for the same program.