首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::nothrow

Defined in header <new>

extern const std::nothrow_t nothrow;

std::nothrow是类型的常数。std::nothrow_t用于消除抛出和不抛的过载的歧义。分配函数...

二次

代码语言:javascript
复制
#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;
        }
    }
}

二次

产出:

二次

代码语言:javascript
复制
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)

代码语言:txt
复制
 © cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券