Order of evaluation of arguments using std::cout

前端 未结 5 1078
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 13:26

Hi all I stumbled upon this piece of code today and I am confused as to what exactly happens and more particular in what order :

Code :

#include &l         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 14:07

    The answer to this question changed in C++17.

    Evaluation of overloaded operators are now sequenced in the same way as for built-in operators (C++17 [over.match.oper]/2).

    Furthermore, the <<, >> and subscripting operators now have the left operand sequenced before the right, and the postfix-expression of a function call is sequenced before evaluation of the arguments.

    (The other binary operators retain their previous sequencing, e.g. + is still unsequenced).

    So the code in the question must now output Value of test is : 0 Return value of function is : 1 Value of test : 1. But the advice "Don't do this" is still reasonable, given that it will take some time for everybody to update to C++17.

提交回复
热议问题