考虑代码
auto p = new T( U(std::move(v)) );
然后初始化程序是 U(std::move(v))
。让我们假设 T( U(std::move(v)) )
没有抛出。如果在底层内存分配之后评估初始化程序,则代码是强异常安全的。否则,它不是。如果抛出内存分配,v
就会被移动。因此,我对内存分配和初始化程序评估之间的相对顺序很感兴趣。它是已定义的、未指定的还是什么?
最佳答案
是的,初始化是在分配之后计算的。引用 C++17 (N4659) [expr.new] 8.3.4/19:
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.
关于c++ - 在 new 表达式中分配内存后是否评估初始值设定项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646113/