Using the post-increment in function arguments

后端 未结 5 966
忘了有多久
忘了有多久 2021-01-12 01:40

When I run this code, the output is 11, 10.

Why on earth would that be? Can someone give me an explanation of this that will hopefully enlighten me?

Thanks<

5条回答
  •  [愿得一人]
    2021-01-12 02:24

    Late answer. Ignoring the issue of the order of evaluation, note that the C++ standard explains how post increment and post decrement operate: " Post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement."

    https://en.cppreference.com/w/cpp/language/operator_incdec

    As an example where the difference in outcome is significant, consider std::list::splice, such as:

        mylist.splice(where, mylist, iter++);
    

    This will move the node pointed by iter to just before the node pointed by where. The sequence will be make a copy of iter to be passed to splice, increment iter, then call splice using the copy of iter before it was incremented. After splice returns, iter will point to the next node after the node iter originally pointed to, as opposed to the next node after iter's new location in the list after it was moved.

提交回复
热议问题