函数参数的销毁顺序是什么? [英] What is the order of destruction of function arguments?

查看:26
本文介绍了函数参数的销毁顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个函数 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.

这篇关于函数参数的销毁顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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