Is initializer evaluated after memory allocation in new expression?

╄→尐↘猪︶ㄣ 提交于 2020-01-24 02:58:13

问题


Consider the code

auto p = new T( U(std::move(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?


回答1:


Yes, the initialisation is evaluated after the allocation. Quoting 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.



来源:https://stackoverflow.com/questions/49646113/is-initializer-evaluated-after-memory-allocation-in-new-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!