C++的new运算符有一个选项,当分配失败时可以返回一个空指针而不是抛出bad_alloc异常。Foo * pf = new(std::nothrow) Foo(1, 2, 3); 是的,我理解这只能防止新对象抛出bad_alloc异常,但它不能防止Foo构造函数抛出异常。如果你想使用shar...
对于新操作符,我们有std::nothrow版本: std::unique_ptr<T> p = new(std::nothrow) T(); 我们是否有类似于std::make_shared或std::make_unique的东西?
有这样的代码: #include <iostream> int main(){ for(;;){ int* ptr = new (std::nothrow) int; if(ptr == 0){ std::cout...