GCC bug? Chaining methods, broken sequence point

后端 未结 3 2160
春和景丽
春和景丽 2021-02-19 03:26

I\'ve been debugging a program for some time, and eventually found the error was due to a reference not being updated as I thought it would be.

Here\'s a example that sh

3条回答
  •  生来不讨喜
    2021-02-19 04:10

    There's no guarantee in C++ about the order in which function arguments in a single expression are evaluated, not even when these functions are chained method calls. You are invoking undefined behavior here and that's what you get.

    The . operator does imply sequencing, but only insofar that the expression before the . has to be fully evaluated before the member is accessed. It doesn't mean that the evaluations of subexpressions are suspended until that point.

    Also, don't pass ints by const int&, there's no way this could be faster than passing an int directly (unless for some strange reason int doesn't fit into a processor word and the reference does).

提交回复
热议问题