std:: nothrow
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Memory management library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Low level memory management
| Functions | ||||
|
(C++11)
|
||||
| Classes | ||||
|
(C++11)
|
||||
|
(C++17)
|
||||
| Types | ||||
| Objects | ||||
|
nothrow
|
||||
|
(C++20)
|
||||
| Object access | ||||
|
(C++17)
|
|
定义于头文件
<new>
|
||
| (1) | ||
|
struct
nothrow_t
{
}
;
|
(C++11 前) | |
|
struct
nothrow_t
{
explicit
nothrow_t
(
)
=
default
;
}
;
|
(C++11 起) | |
|
extern
const
std::
nothrow_t
nothrow
;
|
(2) | |
std::nothrow_t
是一个空类类型,用于区分抛出异常和不抛出异常的
分配函数
的重载版本。
std::nothrow
是其对应的常量对象。
示例
运行此代码
#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
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2510 | C++11 | 默认构造函数为非显式,可能导致歧义 | 改为显式 |
另请参阅
|
分配函数
(函数) |