Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sequence point, function call and undefined behaviour

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading