问题描述
我知道,C ++不指定哪些参数传递给函数的顺序。但是,如果我们编写以下code:
无效__cdecl FUNC(INT A,INT B,INT C)
{
的printf(%D,%D,%D,A,B,C);
}
诠释的main()
{
INT I = 10;
FUNC(++我,我,我++);
}
我们可以肯定地说,输出将 12,11,11
自的确保参数传递顺序是从右到左?
按照标准,有两件事情你需要理解和区分:
Now, please note, __cdecl
ensures only the first, not the second. Calling conventions decide only how the functions arguments will be passed, left-to-right
or right-to-left
; they can still be evaluated in ANY order!
Hope this clarifies your doubts regarding the calling conventions.
However, since these conventions are Microsoft compiler extension to C++, so your code is non-portable. In that case, you can see how MSVC++ compiler evaluates function arguments and be relax IF you don't want to run the same code on other platform!
func(++i, i, ++i);
Note that this particular code invokes undefined behavior, because i
is incremented more than once without any intervening any sequence point.
这篇关于调用约定和评估秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!