yohhoyの日記

技術的メモをしていきたい日記

C/C++関数引数の評価順序

プログラミング言語C/C++では、関数実引数の評価順序は未規定(unspecified)となっている。標準規格における言及箇所のメモ。

2018-02-02追記:C++17仕様では未規定(unspecified)から “不定順の順序付け(indeterminately sequenced)” に変更され、関数実引数の評価順序がより強く規定されるようになった。とはいえ、通常のプログラムにはほとんど影響しない。詳細は id:yohhoy:20180202 参照。

C99

JTC1/SC22/WG14 N1256 6.5.2.2/p10より引用。

10 The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

C11

JTC1/SC22/WG14 N1570 6.5.2.2/p10より引用。

10 There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. 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.94)
脚注94) In other words, function executions do not "interleave" with each other.

C++11

JTC1/SC22/WG21 N3337 5.2.2/p4, p8, 1.9/p13から関連箇所を引用。

4 When a function is called, each parameter (8.3.5) shall be initialized (8.5, 12.8, 12.1) with its corresponding argument. [Note: Such initializations are indeterminately sequenced with respect to each other (1.9) -- end note]
8 [Note: The evaluations of the postfix expression and of the argument expressions are all unsequenced relative to one another. All side effects of argument expression evaluations are sequenced before the function is entered (see 1.9). -- end note]

13 (snip) Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which. [Note: Indeterminately sequenced evaluations cannot overlap, but either could be executed first. -- end note]