C / C ++ - 在函数调用的参数评测 [英] C/C++ - evaluation of the arguments in a function call
问题描述
可能重复:结果
功能参数评测订单
是否可以安全使用下列建设C / C ++?
Is it safe to use the following construction in C/C++?
f(g(), h());
其中,克()
预计将评估第一个,然后 H()
。
做所有的编译器显示在所有的架构相同的行为?
Do all compilers show the same behavior on all architectures?
推荐答案
NO!难保什么顺序这些在进行。只有两克()和h()为F之前进行()。
看到这一点: http://www.gotw.ca/gotw/056.htm
我认为有这方面的一个更新的C ++ 11的版本,我要看看。
NO! There is no guarantee what order these are carried out in. Only that both g() and h() are carried out before f(). See this: http://www.gotw.ca/gotw/056.htm I think there's an updated C++11 version of that, I'll have a look.
编辑:C ++ 11版本的 http://herbsutter.com/gotw/_102/
C++11 version http://herbsutter.com/gotw/_102/
编辑2:如果你真的想知道具体的编译器做,试试这个:的http:/ /www.agner.org/optimize/calling_conventions.pdf
第7章(第16页)可能是相关的,虽然这是一个有点超过我的头,但例如__cdecl调用约定是指参数从右到左(至少那种方式存储的)通过,而对于__fastcall前两个DWORD或更小参数在ECX传递和EDX寄存器;所有其他参数传递从右到左。 ( http://msdn.microsoft.com/ EN-US /库/ 6xa169sk%28V = vs.71%29.aspx )
Edit 2: If you really want to know what specific compilers do, try this: http://www.agner.org/optimize/calling_conventions.pdf Section 7 (page 16) may be relevant, though it's a bit over my head, but for instance __cdecl calling convention means arguments are passed from right to left (at least stored that way), whereas for __fastcall "The first two DWORD or smaller arguments are passed in ECX and EDX registers; all other arguments are passed right to left." (http://msdn.microsoft.com/en-us/library/6xa169sk%28v=vs.71%29.aspx)
所以也因不同的编译器。
So it does vary for different compilers.
很久以后修改:事实证明,使用初始化列表的语法构造函数(大括号 {}
),计算顺序的是的保证(即使它是一个构造函数的调用,这并不需要一个的std :: initializer_list
。<一个href=\"http://stackoverflow.com/questions/14058592/how-to-guarantee-order-of-argument-evaluation-when-calling-a-function-object\">See这个问题。
Much later edit: It turns out that for constructors using the initializer list syntax (curly braces {}
), order of evaluation is guaranteed (even if it is a call to a constructor that does not take a std::initializer_list
. See this question.
这篇关于C / C ++ - 在函数调用的参数评测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!