在线文档教程
C++
应用 | Utilities

std::nothrow

STD:不扔

Defined in header
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_ttag type used to select an non-throwing allocation function (class)
operator newoperator new[]allocation functions (function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Memory/new/nopo