新表达式的评估顺序? [英] Evaluation order of new expression?

查看:160
本文介绍了新表达式的评估顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码示例中,执行C ++标准保证++ i在内存分配(调用operator new)之后,但在调用X的构造函数之前进行评估?

  new X(++ i)


解决方案

从我的n2798副本:


/ p>

21在评估构造函数参数之前或在评估构造函数参数之后,但在输入未指定的构造函数之前调用分配函数。


读取数据库中的参数,如果分配函数返回null指针, (避免歧义):


5.3.4新



8 new-expression通过调用分配函数(3.7.4.1)获取对象的存储。如果newexpression通过抛出异常而终止,它可以通过调用释放函数(3.7.4.2)释放存储。如果分配的类型是非数组类型,则分配函数的名称为operator new,而释放函数的名称为operator delete。如果分配的类型是数组类型,分配
函数的名称是operator new [],而释放函数的名称是operator delete []。 [...]


这几乎回答了这个问题。答案是否。


In the following code sample, do the C++ standard guarantee that '++i' is evaluated after the memory allocation (call to operator new) but before the call to X’s constructor?

new X( ++i )

解决方案

From my copy of n2798:

5.3.4 New

21 Whether the allocation function is called before evaluating the constructor arguments or after evaluating the constructor arguments but before entering the constructor is unspecified. It is also unspecified whether the arguments to a constructor are evaluated if the allocation function returns the null pointer or exits using an exception.

Read in conjunction with (to avoid ambiguities):

5.3.4 New

8 A new-expression obtains storage for the object by calling an allocation function (3.7.4.1). If the newexpression terminates by throwing an exception, it may release storage by calling a deallocation function (3.7.4.2). If the allocated type is a non-array type, the allocation function’s name is operator new and the deallocation function’s name is operator delete. If the allocated type is an array type, the allocation function’s name is operator new[] and the deallocation function’s name is operator delete[]. [...]

This pretty much answers the question. The answer is 'No'.

这篇关于新表达式的评估顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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