std::nothrow
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <new> で定義
|
||
extern const std::nothrow_t nothrow; |
||
std::nothrow は、確保関数の例外を投げるオーバーロードと投げないオーバーロードの曖昧さをなくすために使用される、 std::nothrow_t 型の定数です。
例
Run this code
#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
関連項目
| 例外を投げない確保関数を選択するために使用されるタグ型 (クラス) | |
| 確保関数 (関数) |