std::nothrow
Defined in header <new> | | |
|---|---|---|
extern const std::nothrow_t nothrow; | | |
std::nothrow是类型的常数。std::nothrow_t用于消除抛出和不抛的过载的歧义。分配函数...
例
二次
#include <iostream>
#include <new>
int main()
{
try {
while (true) {
new int[100000000ul]; // throwing overload
}
} catch (const std::bad_alloc& e) {
std::cout << e.what() << '\n';
}
while (true) {
int* p = new(std::nothrow) int[100000000ul]; // non-throwing overload
if (p == nullptr) {
std::cout << "Allocation returned nullptr\n";
break;
}
}
}二次
产出:
二次
std::bad_alloc
Allocation returned nullptr二次
另见
nothrow_t | tag type used to select an non-throwing allocation function (class) |
|---|---|
operator newoperator new[] | allocation functions (function) |
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

