Why does the expression a = a + b - ( b = a ) give a sequence point warning in c++?

后端 未结 4 662
萌比男神i
萌比男神i 2020-11-29 09:02

Following is the test code:

int main()
{
    int a = 3;
    int b = 4;
    a = a + b - (b = a); 

    cout << \"a :\" << a << \" \" <<         


        
4条回答
  •  我在风中等你
    2020-11-29 09:46

    In C++, subexpressions in arithmetic expressions do not have temporal ordering.

    a = x + y;
    

    Is x evaluated first, or y? The compiler can choose either, or it can choose something completely different. The order of evaluation is not the same thing as operator precedence: operator precedence is strictly defined, and order of evaluation is only defined to the granularity that your program has sequence points.

    In fact, on some architectures it is possible to emit code that evaluates both x and y at the same time -- for example, VLIW architectures.

提交回复
热议问题