简体   繁体   English

X()。Y(Z())和标准中的评价顺序

[英]X().Y(Z()) and the order of evaluation in standard

Can somebody quote the corresponding paragraph of the C++ standard that says that the order of the std::string construction and foo() call is unspecified in the following case: 有人可以引用C ++标准的相应段落,它说明在以下情况中未指定std::string构造和foo()调用的顺序:

std::string().append(foo());

I know that there's 5.2.2.8 but it states about function arguments, not several function calls between the same sequence points: 我知道有5.2.2.8,但它说明了函数参数,而不是相同序列点之间的几个函数调用:

The order of evaluation of function arguments is unspecified 函数参数的评估顺序未指定

It was widely believed that leaving the order of expression evaluation undefined led to more optimizations. 人们普遍认为,保留表达评估顺序不确定会导致更多的优化。 It was probably true 10 and 20 years ago, but it does not appear to be so any more. 10年和20年前可能是真的,但它似乎不再如此。 Data to that effect was presented to the committee, but I don't know if it is published anywhere. 这方面的数据已提交给委员会,但我不知道它是否在任何地方发布。

5/4 [expr]: 5/4 [expr]:

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified 除非另有说明,否则单个运算符的操作数和单个表达式的子表达式的评估顺序以及副作用发生的顺序是未指定的。

Because operators are functions, this means string() will be constructued either before or after foo() . 因为运算符是函数,这意味着string()将在foo()之前或之后构造。

Also see: Order of function call 另见: 函数调用顺序

"Bjarne Stroustrup also says it explicitly in "The C++ Programming Language" 3rd edition section 6.2.2, with some reasoning: “Bjarne Stroustrup在”C ++编程语言“第3版第6.2.2节中也明确地说过,有一些推理:

Better code can be generated in the absence of restrictions on expression evaluation order. 在没有对表达式评估顺序的限制的情况下,可以生成更好的代码。

Also see: Order of evaluation in C++ function parameters 另请参阅: C ++函数参数中的评估顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM