在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
):
表达式
E1
在表达式E2
之前排序.
[over.match.oper]/2 :(描述重载的运算符)
操作数按照为内置运算符指定的顺序排序.
[介绍执行]/15:
表达式
X
被说成是之前的序列 (如果每个 值计算和与表达式X
关联的每个副作用在进行每次值计算和与表达式Y
关联的每个副作用之前先进行排序.
在C ++ 17之前,函数调用的顺序是未指定,这意味着它们可以按任意顺序调用(并且在重复调用时此顺序不必相同). /p>
Say for example I have a long statement like
cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n";
would findCurrent()
be run before findLowest()
like logic dictates?
Since C++17 the functions are guaranteed to be called left-to-right, i.e. findCurrent()
is called first, then findLowest()
and so on.
C++17 Standard references: [expr.shift]/4 (referring to the expression E1 << E2
):
The expression
E1
is sequenced before the expressionE2
.
[over.match.oper]/2: (describing overloaded operators)
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
.
Prior to C++17 the order of function calls was unspecified, meaning that they may be called in any order (and this order does not need to be the same on repeated invocations).
这篇关于在cout语句中首先运行什么? (C ++ 17)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!