问题描述
如果某个函数 f
带有参数 p_1
, ..., p_n
类型为 T_1
, ..., T_n
分别用参数 a_1
, ..., a_n
调用,其主体以什么顺序抛出异常、完成或返回论据被破坏了吗?为什么?如果可能,请提供对标准的参考.
If some function f
with parameters p_1
, ..., p_n
of types T_1
, ..., T_n
respectively is called with arguments a_1
, ..., a_n
and its body throws an exception, finishes or returns, in what order are the arguments destroyed and why? Please provide a reference to the standard, if possible.
我实际上想问一下函数参数",但作为 T.C.Columbo 设法消除了我的困惑,我将把这个问题留给论点,并问 一个关于参数的新问题.请参阅对此问题的评论以了解区别.
I actually wanted to ask about function "parameters", but as T.C. and Columbo managed to clear my confusion, I'm leaving this question be about the arguments and asked a new separate question about the parameters. See the comments on this question for the distinction.
推荐答案
标准未指定函数参数的计算顺序.来自 C++11 标准(在线草案):
The order in which the arguments to a function are evaluated is not specified by the standard. From the C++11 Standard (online draft):
5.2.2 函数调用
8 [ 注意: 后缀表达式和参数表达式的求值都是无序的.参数表达式评估的所有副作用都在函数之前排序输入(见 1.9).——尾注 ]
8 [ Note: The evaluations of the postfix expression and of the argument expressions are all unsequenced relative to one another. All side effects of argument expression evaluations are sequenced before the function is entered (see 1.9). —end note ]
因此,完全取决于实现来决定以什么顺序评估函数的参数.反过来,这意味着参数的构造顺序也取决于实现.
Hence, it is entirely up to an implementation to decide in what order to evaluate the arguments to a function. This, in turn, implies that the order of construction of the arguments is also implementation dependent.
一个合理的实现会以与构造相反的顺序销毁对象.
A sensible implementation would destroy the objects in the reverse order of their construction.
这篇关于函数参数的破坏顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!