考虑以下代码行:
new (p++) T();
如果构造函数T()
抛出异常,是否保证p
已经自增?
最佳答案
来自 5.3.4 [expr.new](引自 n3242):
11 The new-placement syntax is used to supply additional arguments to an allocation function. If used, overload resolution is performed on a function call created by assembling an argument list consisting of the amount of space requested (the first argument) and the expressions in the new-placement part of the new-expression (the second and succeeding arguments).
所以在一个新的表达式中,分配函数是从函数调用中使用的(这是有道理的)。所有分配函数都是函数,包括实现提供的函数,来自 3.7.4.1 [basic.stc.dynamic.allocation]:
1 An allocation function shall be a class member function or a global function; [...]
所以当构造函数抛出异常时,分配已经发生并且相关的函数调用表达式已经被完全评估。
关于c++ - Placement-new 是否引入了序列点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6496912/