C++ 函数参数中的求值顺序 [英] Order of evaluation in C++ function parameters

查看:41
本文介绍了C++ 函数参数中的求值顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有三个函数(foo、bar 和 baz)像这样组合...

If we have three functions (foo, bar, and baz) that are composed like so...

foo(bar(), baz())

C++ 标准是否保证 bar 会在 baz 之前被评估?

Is there any guarantee by the C++ standard that bar will be evaluated before baz?

推荐答案

不,没有这样的保证.根据 C++ 标准,它是未指定的.

No, there's no such guarantee. It's unspecified according to the C++ standard.

Bjarne Stroustrup 在The C++ Programming Language"第 3 版第 6.2.2 节中也明确说明了这一点,并附有一些推理:

Bjarne Stroustrup also says it explicitly in "The C++ Programming Language" 3rd edition section 6.2.2, with some reasoning:

更好的代码可以在表达没有限制评价顺序

Better code can be generated in the absence of restrictions on expression evaluation order

尽管从技术上讲,这指的是同一部分的较早部分,该部分表示表达式各部分的求值顺序也未指定,即

Although technically this refers to an earlier part of the same section which says that the order of evaluation of parts of an expression is also unspecified, i.e.

int x = f(2) + g(3);   // unspecified whether f() or g() is called first

这篇关于C++ 函数参数中的求值顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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