在cout语句中首先运行什么? (C ++ 17) [英] What is run first inside a cout statement? (C++17)

查看:75
本文介绍了在cout语句中首先运行什么? (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关联的每个副作用之前先进行排序.

链接到cpp引用摘要


在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 expression E2.

[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 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.

Link to cppreference summary


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆