函数参数求值顺序 [英] function parameter evaluation order

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

问题描述

在 C 和 C++ 中,对函数的参数求值是否有固定的顺序?我的意思是,标准怎么说?是left-to-right 还是right-to-left?我从书中得到了令人困惑的信息.

函数调用是否必须使用stack only来实现?C 和 C++ 标准对此有何评论?

解决方案

C 和 C++ 是两种完全不同的语言;不要假设相同的规则总是适用于两者.然而,在参数评估顺序的情况下:

C99:

<块引用>6.5.2.2 函数调用
...
10 函数指示符的求值顺序、实际参数和实参中的子表达式未指定,但有一个序列点在实际通话之前.

[编辑]C11(草案):

<块引用>6.5.2.2 函数调用
...
10 在函数指示符和实际函数的评估之后有一个序列点参数但在实际调用之前.调用函数中的每个评估(包括其他函数调用)之前或之后没有特别排序的被调用函数体的执行是不确定的被调用函数的执行.94)
...
94) 换句话说,函数执行不会相互交错".

C++:

<块引用>5.2.2 函数调用
...
8 参数的评估顺序未指定.参数表达式评估的所有副作用生效在进入函数之前.后缀表达式和参数表达式列表的计算顺序是未指定.

两个标准都没有强制要求使用硬件堆栈来传递函数参数;这是一个实现细节.C++ 标准使用术语展开堆栈"来描述为从 try 块到 throw-expression 的路径上自动创建的对象调用析构函数,但仅此而已.大多数流行的架构确实通过硬件堆栈传递参数,但它并不通用.

[编辑]

<块引用>

我从书中得到了令人困惑的信息.

这一点也不奇怪,因为很容易 90% 的关于 C 的书籍都是废话.

虽然语言标准对于学习 C 或 C++ 而言都不是很好的资源,但对于此类问题来说,手头还是有好处的.官方™标准文档需要花钱,但也有在线免费提供的草稿,对于大多数用途来说应该足够了.

最新的 C99 草案(自原始发布以来有更新)可用 此处.最新的预发布 C11 草案(去年正式批准)可在 此处.并且可以使用 C++ 语言的公开草案在这里,虽然它有一个明确的免责声明,即某些信息不完整或不正确.

In C and C++, is there a fixed order for evaluation of parameter to the function? I mean, what do the standards say? Is it left-to-right or right-to-left? I am getting confusing information from the books.

Is it necessary that function call should be implemented using stack only? What do the C and C++ standards say about this?

解决方案

C and C++ are two completely different languages; don't assume the same rules always apply to both. In the case of parameter evaluation order, however:

C99:

6.5.2.2 Function calls
...
10 The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

[Edit] C11 (draft):

6.5.2.2 Function calls
...
10 There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.94)
...
94) In other words, function executions do not ‘‘interleave’’ with each other.

C++:

5.2.2 Function call
...
8 The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations take effect before the function is entered. The order of evaluation of the postfix expression and the argument expression list is unspecified.

Neither standard mandates the use of the hardware stack for passing function parameters; that's an implementation detail. The C++ standard uses the term "unwinding the stack" to describe calling destructors for automatically created objects on the path from a try block to a throw-expression, but that's it. Most popular architectures do pass parameters via a hardware stack, but it's not universal.

[Edit]

I am getting confusing information from the books.

This is not in the least surprising, since easily 90% of books written about C are simply crap.

While the language standard isn't a great resource for learning either C or C++, it's good to have handy for questions like this. The official™ standards documents cost real money, but there are drafts that are freely available online, and should be good enough for most purposes.

The latest C99 draft (with updates since original publication) is available here. The latest pre-publication C11 draft (which was officially ratified last year) is available here. And a publicly availble draft of the C++ language is available here, although it has an explicit disclaimer that some of the information is incomplete or incorrect.

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

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