Order of evaluation
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
评价任何C运算符,包括评价函数的参数在函数调用表达式的顺序,并在任何表达式中的子表达式的计算顺序的操作数的顺序是不确定的(除非特别注明,以下)。编译器将对其进行评估,以任何顺序,相同的表达式时,可以选择另一种秩序再次被评为.
原文:
Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below). The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.
的概念不存在左向右或由右至左的评价C,这是不被混淆与左到右,右到左的运营商之间的关联性的表达
a + b + c
被解析为(a + b) + c
由于在运行时,左到右运营商+之间的关联性进行评估,但该子表达式c
第一(或最后一个,或在同一时间a
或b
).原文:
There is no concept of left-to-right or right-to-left evaluation in C, which is not to be confused with left-to-right and right-to-left associativity of operators: the expression
a + b + c
is parsed as (a + b) + c
due to left-to-right associativity of operator+, but the subexpression c
may be evaluated first (or last, or at the same time as a
or b
) at run time.目录 |
[编辑] 定义
[编辑] 评估
有两种类型的评价由编译器执行的每个表达式或子表达式(这两者都是可选的):
原文:
There are two kinds of evaluations performed by the compiler for each expression or subexpression (both of which are optional):
- “数值计算”表达式返回的值来计算的。这可能涉及的对象的身份(左值评价)测定或读取先前分配到一个对象(右值评价)的值的原文:value computation: calculation of the value that is returned by the expression. This may involve determination of the identity of the object (lvalue evaluation) or reading the value previously assigned to an object (rvalue evaluation)
- “副作用”:挥发性glvalue,修改(写)到指定的对象,修改文件,修改的浮点环境(如果支持的话),或者调用一个函数,对象的访问(读或写)做任何操作.原文:side effect: access (read or write) to an object designated by a volatile glvalue, modification (writing) to an object, modifying a file, modifying the floating-point environment (if supported), or calling a function that does any of those operations.
如果没有副作用,是由一个表达式,编译器可以判断的价值不使用,该表达式评估.
原文:
If no side effects are produced by an expression and the compiler can determine that the value is not used, the expression is not evaluated.
[编辑] 订购
“测序之前”是一个非对称的,传递的,对在同一个线程(如果涉及原子类型和记忆障碍,它可能会延长跨线程)的评价之间的关系.
原文:
"sequenced-before" is an asymmetric, transitive, pair-wise relationship between evaluations within the same thread (it may extend across threads if atomic types and memory barriers are involved).
- 如果一个序列点之间存在的子表达式E1和E2,E1的值的计算和副作用测序前的每一个值的计算和副作用的E2原文:If a sequence point is present between the subexpressions E1 and E2, then both value computation and side effects of E1 are sequenced before every value computation and side effect of E2
- 如果评价一个前评价B测序,然后将评价一个完整的评价的B开始之前。 (C11 起)原文:If evaluation A is sequenced before evaluation B, then evaluation of A will be complete before evaluation of B begins. (C11 起)
- 如果A前B和B的前一个测序测序,然后评价的B是A开始之前完成评估。 (C11 起)原文:If A is not sequenced before B and B is sequenced before A, then evaluation of B will be complete before evaluation of A begins. (C11 起)
- 如果A没有B和B测序之前不测序A之前,则有两种可能性存在:(C11 起)原文:If A is not sequenced before B and B is not sequenced before A, then two possibilities exist: (C11 起)
- *编序的A和B的评价:他们可能会以任何顺序进行,并可能会重叠(在一个线程中执行,编译器可能会交织的CPU指令,包括A和B)原文:* evaluations of A and B are unsequenced: they may be performed in any order and may overlap (within a single thread of execution, the compiler may interleave the CPU instructions that comprise A and B)
- * A和B的评价是不能决定排序:它们可能以任何顺序进行,但不能重叠:无论是A在B之前完成,或B之前完成A.可能是相反的顺序下一次相同表达式求值.原文:* evaluations of A and B are indeterminably-sequenced: they may be performed in any order but may not overlap: either A will be complete before B, or B will be complete before A. The order may be the opposite the next time the same expression is evaluated.
[编辑] 规则
1)有一个序列点后的评价函数的所有参数和功能代号,和实际的函数调用前.
2) 原文:
There is a sequence point after the evaluation of all function arguments and of the function designator, and before the actual function call.
任何运算符的操作数的值计算(但不是副作用)测序之前的操作的结果(而不是其副作用)的值计算。 (C11 起)
3) 原文:
The value computations (but not the side-effects) of the operands to any operator are sequenced before the value computation of the result of the operator (but not its side-effects). (C11 起)
有一个序列点后的第一个(左)操作的评估和评价以下二元运算符的第二个(右)操作前:
4) &&
(逻辑与),||
(逻辑或),和,
(逗号).原文:
There is a sequence point after evaluation of the first (left) operand and before evaluation of the second (right) operand of the following binary operators:
&&
(logical AND), ||
(logical OR), and ,
(comma).有一个序列点后的第一个(左)操作的评估,评估前的第二个或第三个操作数(以较早者为准计算)条件运算符
5) ?:
原文:
There is a sequence point after evaluation of the first (left) operand and before evaluation of the second or third operand (whichever is evaluated) of the conditional operator
?:
副作用(左参数的修改)的直接赋值操作符和复合赋值操作符的所有测序后,左,右参数的值计算(但不包括副作用)。 (C11 起)
6) 原文:
The side effect (modification of the left argument) of the direct assignment operator and of all compound assignment operators is sequenced after the value computation (but not the side effects) of both left and right arguments. (C11 起)
测序的的递增后和postdecrement运营商的值计算之前,它的副作用。 (C11 起)
7) 原文:
The value computation of the postincrement and postdecrement operators is sequenced before its side-effect. (C11 起)
是在一个完整的说明符的结束点的序列。 (C99 起)
8) 原文:
There is a sequence point at the end of a full declarator. (C99 起)
有一个序列点后评价一个完整的表达式(表达式,它是不是一个子表达式:典型的结束与一个的分号或一个控制语句如果/开关/而/)和先下一充分体现.
9) 原文:
There is a sequence point after the evaluation of a full expression (an expression that is not a subexpression: typically something that ends with a semicolon or a controlling statement of if/switch/while/do) and before the next full expression.
有一个序列点之前返回库函数。 (C99 起)
10) 原文:
There is a sequence point immediately before the return of a library function. (C99 起)
有一个序列点后的行动与每个转换说明符格式化I / O (C99 起)
11) 原文:
There is a sequence point after the action associated with each conversion specifier in formatted I/O. (C99 起)
有序列点之前和之后每个调用的库函数qsort和bsearch之间,以及在任何呼叫的比较功能和相关联的对象的运动由qsort(C99 起)由一个比较函数
12) 不定测序,测序前或后测序另一个函数调用一个函数调用(CPU指令不能交错,构成不同的函数调用的功能,即使是内联)(C11 起)
13) 原文:
A function call that is not sequenced before or sequenced after another function call is indeterminately sequenced (CPU instructions that constitute different function calls cannot be interleaved, even if the functions are inlined) (C11 起)
在初始化列表表达式中,所有的评估都犹豫不决测序(C11 起)
14) 原文:
In initialization list expressions, all evaluations are indeterminately sequenced (C11 起)
对于不定排序的函数调用,操作的复合赋值操作符,前缀和后缀形式的递增和递减运算是单一的评价。 (C11 起)
原文:
With respect to an indeterminately-sequenced function call, the operation of compound assignment operators, and both prefix and postfix forms of increment and decrement operators are single evaluations. (C11 起)
[编辑] 未定义的行为
1)如果一个副作用,在标量对象的编序相对于在同一个标量对象的另一种副作用,该行为是未定义.
原文:
If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined.
i = ++i + i++; // undefined behavior
i = i++ + 1; // undefined behavior
f(++i, ++i); // undefined behavior
f(i = -1, i = -1); // undefined behavior
如果一个副作用,在标量对象的编序的相对值,使用相同的标量对象的值的计算,该行为是未定义.
原文:
If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined.
f(i, i++); // undefined behavior
a[i] = i++; // undefined bevahior
上述规则中的至少一个允许的子表达式的顺序允许这种非循序副作用只要适用.
原文:
The above rules apply as long as at least one allowable ordering of subexpressions permits such an unsequenced side-effect.