C++:用户定义的逗号运算符的求值顺序是什么?

C++: What is the evaluation order of the user-defined comma operator?

我在阅读 Nicolai Josuttis 的“C++ 17 Completed Guide”时遇到了以下表达式:

foo(arg1), (foo(arg2), foo(arg3));

作者声称内置逗号运算符的计算顺序将从左到右,但可以通过重载它们来更改。但是,我看到了关于 cppreference (https://en.cppreference.com/w/cpp/language/eval_order) 的“评估顺序”一文,并遇到了以下陈述:

  1. Every value computation and side effect of the first (left) argument of the built-in comma operator , is sequenced before every value computation and side effect of the second (right) argument.

  1. Every overloaded operator obeys the sequencing rules of the built-in operator it overloads when called using operator notation.

因此,根据声明 16,cppreference 似乎声称重载的逗号运算符与其内置对应项具有相同的评估顺序。 那么,作者所说的“通过重载逗号运算符,您可以更改它的计算顺序”到底是什么意思?预期的行为是什么?

C++17 之前的求值顺序一团糟。 C++17 对计算顺序进行了彻底的更改,这很可能只是作者的一个错误。

在 C++17 之前,重载运算符是完整的语法糖。使用任何二元运算符 @a@b is equivalent

之一
operator@(a, b)
a.operator@(b)

取决于它是成员函数还是自由函数。也就是说,ab 相对于彼此是无序的

在 C++17 中 a@b is the same as the built-in operators 的计算顺序。对于逗号运算符,a 排在 b 之前。在函数调用版本中,ab 的顺序不确定。