功能参数评测秩序 [英] function parameter evaluation order

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

问题描述

C / C ++ ,是有参数的评价功能固定的顺序?我的意思是,你的标准说什么?它是左到右从右至左
我正在从书本上混乱的信息。

有必要的函数调用应该使用实施栈只有。什么是 C / C ++ 标准,说这个?


解决方案

C和C ++是两个完全不同的语言;不承担相同的规则总是适用于两者。在参数评测秩序的情况下,但是:

C99:


6.5.2.2函数调用的结果
...结果
10功能指示符,实际的论点评估的秩序,
  实际的参数内SUBEX pressions是不确定的,但有一个序列点
   前实际调用。

[修改的]
C11(草案):


6.5.2.2函数调用的结果
...结果
10有功能标志和实际的评估后的序列点
论点,但实际的呼叫。每个评估调用函数(包括
其它函数调用的)不是之前或之后,另有具体测序
被调用的函数的主体的执行indeterminately相对于测序
执行被调用函数的。 94)结果
...结果
94)换句话说,功能执行不''交织''彼此。

C ++


5.2.2函数调用的结果
...结果
8参数评估的顺序是不确定的。参数前pression评估的所有副作用生效
 前输入的功能。后缀前pression的评估和论证前pression列表的顺序是
  不确定的。

无论是标准要求使用硬件堆栈传递函数的参数;这是一个实现细节。 C ++标准使用术语展开堆栈来形容,要求自动创建的对象的析构函数从路径上的尝试块一个的抛出-EX pression 的,但仅此而已。最流行的架构的的通过硬件堆栈传递参数,但它不具有普遍性。

[修改的]


  

我从书本上获取混乱的信息。


这是丝毫不奇怪,因为容易的90%的写C语言的书籍是简单的扯淡的。

虽然语言标准是不适合的学习的C或C ++一个很好的资源,这是很好的有得心应手这样的问题。官方及贸易;标准文档花费真金白银,但也有草稿是免费提供的联机,应该是多数已经足够了。

最新的C99草案(自与原发布更新),可这里。最新的pre-C11公布草案(这是官方去年批准)可用的此处。和C ++语言的公开availble的草案提供这里 ,虽然它有一个明确的声明,一些信息不完整或不正确。

In C/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 does the C/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天全站免登陆