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

前端 未结 5 663
野的像风
野的像风 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:00

    That is because, it is unrelated to operator precedence. The post-increment operator ++, as opposed to the pre-increment operator, increments its operand after its evaluation.

    So what you see is normal and that behavior cannot be changed by introducing enclosing parenthesis around the variable. If you want the opposite to happen, then you should use the pre-increment operator like the following:

    cout << ++x << endl;
    
    0 讨论(0)
  • 2021-01-29 16:06

    This is because x++ evaluates the value of x (5 in your case) and will increase its value after that....

    what you are looking for is the pre-increment, also ++x

    0 讨论(0)
  • 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...

    0 讨论(0)
  • 2021-01-29 16:16

    The expression (x++), with or without parentheses, evaluates to the previous value of x, and has the side-effect of increasing x.

    If you want to see the effect of the increase then use the obscure

    cout << (x++, x) << endl;
    
    0 讨论(0)
  • 2021-01-29 16:21

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

    Operator precedence has nothing to do with this.

    The misunderstanding probably isn't your fault: you've likely been mistaught. Your teacher(s) told you that an operand with a higher precedence, than some other operand, will be "executed first".

    While this is a common explanation in schools, it is not true.

    There are three things that can change the meaning of an expression in this sense:

     

    1. Operator precedence

      This is merely a set of rules that tell us, and tell the compiler, which operands go to which operator. Like, in 3 + 5 * 7, do we pass 3+5 to the multiplication operator, or do we pass 5*7 to the addition operator? It's about parsing.

       

    2. Evaluation order

      Each operand then needs to be evaluated to produce a value (e.g. 3+5 becomes 8, or 5*7 becomes 35). The rules on the order in which these evaluations happen are quite complicated in C++, more so than you might expect, but you usually don't have to worry about them unless you're doing crazy things in between sequence points (to borrow pre-C++11 parlance).

      (This is the closest you'll get to a notion of "will be executed first".)

       

    3. The meaning of the operator

      This is where you're coming unstuck here. The meaning of the postfix increment operator x++ is "increment x, and evaluate to the old value". Period. Full stop.

      It doesn't matter which operator precedence rules led to the expression x++ being evaluated (as opposed to some other interpretation of the symbols in your code): when it's evaluated, whenever it's evaluated, you get the old value for x.

      The meaning of the prefix increment operator ++x, however, is "increment x, and evaluate to the new value", and that's the behaviour you want, so that's the code you should write.

       

    Ultimately, what sequence of computer instructions actually produces this behaviour is completely up to the compiler, and can be surprising. You shouldn't worry about it, as long as the program's result is as specified in the standard.

    So just forget about this "will be executed first" stuff; it's rubbish.

    0 讨论(0)
提交回复
热议问题