其他分享
首页 > 其他分享> > c-参数顺序评估

c-参数顺序评估

作者:互联网

在该标准的早期版本(C 03)中,未指定对函数调用的参数评估顺序.

在标准的后续版本(C 11或C 14)中是否对此进行了更改?
即我们是否可以依赖特定的顺序(从左到右).

解决方法:

不,这没有改变,但是最近有一个更改提议:N4228: Refining Expression Evaluation Order for
Idiomatic C++
,这是Pre-Urbana mailing that came out this October的一部分引言说(强调我的前进):

Expression evaluation order is a recurring discussion topic in the
C++ community. In a nutshell, given an expression such as f(a, b, c),
the order in which the sub-expressions f , a , b , c are evaluated
is left unspecified by the standard.
If any two of these
sub-expressions happen to modify the same object without intervening
sequence points, the behavior of the program is undefined. For
instance, the expression f(i++, i) where i is an integer variable
leads to undefined behavior

它建议:

We propose to revise C++ evaluation rules to support decades-old
idiomatic constructs and programming practices. A simple solution
would be to require that every expression has a well-defined
evaluation order. That suggestion has traditionally met resistance for
various reasons. Rather, this proposes suggests a more targeted fix

  • Postfix expressions are evaluated from left to right. This includes
    functions calls and member section expressions
    .
  • Assignment expressions are evaluated from right to left. This includes compound assignments.
  • Operands to shift operators are evaluated from left to right

更新

Herb Sutter最近put out a poll on order of evaluation希望从社区中获得一些对我们期望从以下代码中获得什么结果的反馈:

std::vector<int> v = { 0, 0 };
int i = 0;
v[i++] = i++;
std::cout << v[0] << v[1] << endl;

这似乎表明委员会正在认真考虑评估顺序的问题,但是正如我们从讨论中看到的那样,这是有争议的.

标签:c,operator-precedence,parameters
来源: https://codeday.me/bug/20191009/1880732.html