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

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

问题描述

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

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

foo(bar(), baz())

before baz?

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

推荐答案

不,没有这样的保证。

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

Bjarne Stroustrup在C ++编程语言第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 are also undefined, i.e.

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

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

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