std::nothrow
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <new>
  | 
||
|   extern const std::nothrow_t nothrow;  | 
||
std::nothrow 是 std::nothrow_t 类型的常量,用于区分抛出与不抛出分配函数的重载。
示例
运行此代码
#include <iostream> #include <new> int main() { try { while (true) { new int[100000000ul]; // 抛出重载 } } catch (const std::bad_alloc& e) { std::cout << e.what() << '\n'; } while (true) { int* p = new(std::nothrow) int[100000000ul]; // 不抛出重载 if (p == nullptr) { std::cout << "Allocation returned nullptr\n"; break; } } }
输出:
std::bad_alloc Allocation returned nullptr
参阅
|    用于选择不抛出分配函数的标签类型  (类)  | |
|    分配函数   (函数)  |