C ++中的编译器和参数顺序 [英] Compilers and argument order of evaluation in C++

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

问题描述

好的,我知道,标准规定,C ++实现可以选择函数的参数在哪个顺序参数进行评估,但是有任何实现,实际上利用的情况下,它实际上会影响程序?

Okay, I'm aware that the standard dictates that a C++ implementation may choose in which order arguments of a function are evaluated, but are there any implementations that actually 'take advantage' of this in a scenario where it would actually affect the program?

典型示例:

int i = 0;
foo(i++, i++);

注意:我不是在寻找有人告诉我评价的顺序不能依赖,我很清楚这一点。我只想感兴趣的是,任何编译器实际上是否从左到右的顺序评估,因为我的猜测是,如果他们做了很多写得不好的代码会破坏(正确的,但他们仍然可能抱怨)。

Note: I'm not looking for someone to tell me that the order of evaluation can't be relied on, I'm well aware of that. I'm only interested in whether any compilers actually do evaluate out of a left-to-right order because my guess would be that if they did lots of poorly written code would break (rightly so, but they would still probably complain).

推荐答案

这取决于参数类型,被调用函数的调用约定,archtecture和编译器。在x86上, Pascal 调用约定从左到右计算参数,而在C调用约定中 __cdecl ),它是正确的左边。大多数在多个平台上运行的程序都会考虑调用约定来跳过惊喜。

It depends on the argument type, the called function's calling convention, the archtecture and the compiler. On an x86, the Pascal calling convention evaluates arguments left to right whereas in the C calling convention (__cdecl) it is right to left. Most programs which run on multiple platforms do take into account the calling conventions to skip surprises.

有一个不错的文章如果您有兴趣,可以在Raymond Chen的博客上发表。您还可以查看GCC手册的堆栈和调用部分。

编辑:只要我们分割头发:我的回答不是作为一个语言问题,而是作为一个平台。语言标准不会显示或偏好一个而不是另一个,并保留为未指定。注意措辞。它不说这是未定义的。在这种意义上未指定意味着你不能指望的东西,非便携式的行为。我没有C规范/草案方便,但它应该类似于我的n2798草案(C ++)

So long as we are splitting hairs: My answer treats this not as a language question but as a platform one. The language standard does not gurantee or prefer one over the other and leaves it as unspecified. Note the wording. It does not say this is undefined. Unspecified in this sense means something you cannot count on, non-portable behavior. I don't have the C spec/draft handy but it should be similar to that from my n2798 draft (C++)


某些其他方面并且在该国际标准中将抽象机的操作描述为未指定的(例如,对函数的参数的求值顺序)。在可能的情况下,本国际标准定义了一组允许的行为。这些定义了抽象机的非确定性方面。抽象机的实例因此可以具有用于给定程序和给定输入的多于一个可能的执行序列。

Certain other aspects and operations of the abstract machine are described in this International Standard as unspecified (for example, order of evaluation of arguments to a function). Where possible, this International Standard defines a set of allowable behaviors. These define the nondeterministic aspects of the abstract machine. An instance of the abstract machine can thus have more than one possible execution sequence for a given program and a given input.

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

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