表达式f()>g()的值,当f&g修改同一个全局变量undefined或unspecified时?

Is the value of expression f() > g(), when f & g modify same global variable undefined or unspecified?

UPDATE:正如用户 ecatmur 所标记的,它是 In C99, is f()+g() undefined or merely unspecified? 的副本(尽管问题询问的是 C99,但 C++ 的答案不变)。答案是:未指定(对于这两种情况)。


考虑以下 C++14 代码片段:

int i = 0;
int x() { i++; return i;}
int y() { i++; return i;}
bool z = (x() > y());  // unspecified or undefined ?

z 的值只是 未指定,还是未定义的行为?

根据我的理解(如果我错了请更正),这种表达式:i++ > i++ 将是未定义的行为,因为我们在一对序列点之间对同一个变量进行两次变异,但是什么关于上面的案例(突变发生在不同的函数中)?

那这个呢:

bool z = (x() > i++);  // undefined or unspecified now ?

这是未指定的行为。

参考:http://en.cppreference.com/w/cpp/language/eval_order

The order of evaluation of the subexpressions within any expression is unspecified

bool z = (x() > y());bool z = (x() > i++); 都是 未指定的 行为。
您唯一可以做的假设是 x() 并且 y() 将在 之前 > 被处理。
没有其他任何保证。

在这两种情况下,值都是未指定的,但行为是明确定义的。函数调用相对于调用它们的表达式中的其他计算不确定地排序,如 [intro.exececution] 1.9/15:

中指定

Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function

所以对 i 的所有访问都是按顺序进行的,给出了定义明确的行为,但顺序是不确定的,给出了一个未指定的值。