new (nothrow)操作符实际上是什么意思,为什么要使用它

What does new (nothrow) operator actually means and why should we use it?

本文关键字:为什么 意思 是什么 nothrow 操作符 实际上 new      更新时间:2023-10-16

请参阅c++工作草案

T* p1 = new T; // throws bad_alloc if it fails
T* p2 = new(nothrow) T; // returns 0 if it fails

但是在什么情况下new运算符抛出(或者在new(nothrow)的情况下返回0)

在什么情况下new操作符抛出(或者new(nothrow)返回0)?

当没有足够的可用内存来分配对象时,
或者如果新对象的构造函数本身抛出异常
(在这种情况下,即使是new的nothrow-变体也会抛出,
ie。