参数顺序评价 [英] Parameter order evaluation
问题描述
在标准的以前版本(C ++ 03)中,函数调用的参数评估顺序未指定。
In previous versions of the standard (C++03) the order of evaluation of parameters to a function call was unspecified.
在后续版本中已更改的标准(C ++ 11或C ++ 14)?
ie我们可以依靠一个特定的顺序(从左到右)或不是。
Has this been changed in subsequent version of the standard (C++11 or C++14)?
i.e. Can we rely on a specific ordering (left to right) or not.
推荐答案
这没有改变,但最近有一个建议要改变: N4228:为
精炼表达式求值顺序习惯C ++ ,这是今年10月出版的前Urbana邮件介绍说(强调我的未来 p>
No this has not changed but there is a very recent proposal to change this: N4228: Refining Expression Evaluation Order for Idiomatic C++, this was part of the Pre-Urbana mailing that came out this October The introduction says (emphasis mine going forward):
表达式求值顺序是
C ++社区中定期讨论的主题。简而言之,给定一个表达式,例如f(a,b,c),
评估子表达式f,a,b,c的顺序
未指定。如果这些
子表达式中的任意两个碰巧修改同一个对象而没有插入
序列点,则程序的行为是未定义的。对于
实例,表达式f(i ++,i)其中i是整数变量
导致未定义的行为
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
它建议:
我们建议修改C ++评估规则以支持几十年前的
惯用的构造和编程实践。一个简单的解决方案
是要求每个表达式都有一个定义良好的
评估顺序。这个建议传统上由于各种原因遇到阻力。相反,此建议提出了更有针对性的修复
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
- 后缀表达式从左到右计算。 这包括
函数调用和成员节表达式。 - 从右到左计算赋值表达式。
更新
Herb Sutter最近对评估顺序进行投票寻找社区的一些反馈我们希望从下面的代码中得到什么结果:
Herb Sutter recently put out a poll on order of evaluation looking for some feedback from the community on what result we would expect from the following code:
std::vector<int> v = { 0, 0 };
int i = 0;
v[i++] = i++;
std::cout << v[0] << v[1] << endl;
这似乎表明委员会正在严肃地审视评估顺序的话题,从讨论中看到这是有争议的。
This would seem to indicate the committee is looking at the topic of order of evaluation seriously but as we can see from the discussion this is controversial.
这篇关于参数顺序评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!