gpt4 book ai didi

c++ - std::nothrow 几乎没用?

转载 作者:行者123 更新时间:2023-11-28 00:28:18 30 4
gpt4 key购买 nike

<分区>

Nothrow constant: This constant value is used as an argument for operator new and operator new[] to indicate that these functions shall not throw an exception on failure, but return a null pointer instead.

但是在这个简单的例子中,new 没有返回 NULL,而是抛出异常:

struct SomeStruct
{
SomeStruct()
{
std::bad_alloc exception;
throw exception;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
SomeStruct* somestruct;
somestruct = new (std::nothrow) SomeStruct;
return 0;
}

有什么解释吗?它只是表明,尽管 new(std::nothrow) 参数,我们仍然需要将代码放在 try...catch block 中.

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com