cout 语句中的 运行 是什么? (C++17)
What is run first inside a cout statement? (C++17)
比如说我有一个很长的语句,比如
cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n";
findCurrent()
会像逻辑指示的那样在 findLowest()
之前 运行 吗?
自 C++17 起,函数保证从左到右调用,即先调用 findCurrent()
,然后调用 findLowest()
,依此类推。
C++17标准参考:[expr.shift]/4(指表达式E1 << E2
):
The expression E1
is sequenced before the expression E2
.
[over.match.oper]/2:(描述重载运算符)
the operands are sequenced in the order prescribed for the built-in operator.
[intro.execution]/15:
An expression X
is said to be sequenced before an expression Y
if every
value computation and every side effect associated with the expression X
is sequenced before every value computation and every side effect associated with the expression Y
.
在 C++17 之前,函数调用的顺序是 未指定的,这意味着它们可以以任何顺序调用(并且这个顺序不需要与重复调用)。
在 C++17 之前,the order of evaluation is unspecified。
从 C++17 开始,它需要从左到右计算。标准报价见。
比如说我有一个很长的语句,比如
cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n";
findCurrent()
会像逻辑指示的那样在 findLowest()
之前 运行 吗?
自 C++17 起,函数保证从左到右调用,即先调用 findCurrent()
,然后调用 findLowest()
,依此类推。
C++17标准参考:[expr.shift]/4(指表达式E1 << E2
):
The expression
E1
is sequenced before the expressionE2
.
[over.match.oper]/2:(描述重载运算符)
the operands are sequenced in the order prescribed for the built-in operator.
[intro.execution]/15:
An expression
X
is said to be sequenced before an expressionY
if every value computation and every side effect associated with the expressionX
is sequenced before every value computation and every side effect associated with the expressionY
.
在 C++17 之前,函数调用的顺序是 未指定的,这意味着它们可以以任何顺序调用(并且这个顺序不需要与重复调用)。
在 C++17 之前,the order of evaluation is unspecified。
从 C++17 开始,它需要从左到右计算。标准报价见