std::nothrow
来自cppreference.com
工具库
动态内存管理
|
低层内存管理
函数 | ||||
(C++11)
|
||||
类 | ||||
(C++11)
|
||||
(C++17)
|
||||
类型 | ||||
对象 | ||||
nothrow
|
||||
(C++20)
|
||||
对象访问 | ||||
(C++17)
|
在标头
<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
参阅
用于选择不抛出分配函数的标签类型 (类) |
|
分配函数 (函数) |