在新表达式中分配内存后是否对初始化程序求值? [英] Is initializer evaluated after memory allocation in new expression?
问题描述
考虑代码
auto p = new T( U(std::move(v)) );
则初始化器为U(std::move(v))
.假设T( U(std::move(v)) )
没有抛出.如果在基础内存分配之后对初始化程序进行了评估,则该代码将是强异常安全的.否则,事实并非如此.如果抛出了内存分配,则v
应该已经被移动了.因此,我对内存分配和初始化程序评估之间的相对顺序感兴趣.它是定义的,未指定的还是什么?
The initializer is then U(std::move(v))
. Let's assume that T( U(std::move(v)) )
does not throw. If the initializer is evaluated after the underlying memory allocation, the code is then strong-exception-safe. Otherwise, it is not. Had memory allocation thrown, v
would have already been moved. I'm therefore interested in the relative order between memory allocation and initializer evaluation. Is it defined, unspecified, or what?
推荐答案
是的,初始化是在分配后进行评估的.引用C ++ 17(N4659)[expr.new] 8.3.4/19:
Yes, the initialisation is evaluated after the allocation. Quoting C++17 (N4659) [expr.new] 8.3.4/19:
分配函数的调用在 new-initializer 中的表达式求值之前进行排序. 分配的对象的初始化在 new-expression 的值计算之前进行排序.
The invocation of the allocation function is sequenced before the evaluations of expressions in the new-initializer. Initialization of the allocated object is sequenced before the value computation of the new-expression.
这篇关于在新表达式中分配内存后是否对初始化程序求值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!