why parenthesis is unable to change c++ operator precedence in this case?

前端 未结 5 741
野的像风
野的像风 2021-01-29 15:24

Here\'s my simple code:

int main()
{
    int x = 5;
    cout << (x++) << endl;

    return 0;
}

the code above prints 5

5条回答
  •  广开言路
    2021-01-29 16:10

    The value of x++ is the value of x before incrementing, no matter how many brackets you put. This has nothing to do with operator precendence, but this is just how post increment is defined.

    edit: i definitely understand ++x guys, my question is about change operator precedence using ()

    I already mentioned it, but to be clear: The value you see has little to do with operator precedence. With or without brackets ++ comes before <<. Even if this wasnt the case it would not change the value you get from x++. You could change order of the operators if you wrote

    (cout << x)++ << endl;
    

    but that would try to call ++ on the stream...

提交回复
热议问题