std::nothrow
From cppreference.com
| ヘッダ <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]; // 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
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2510 | C++11 | デフォルトコンストラクタが非明示的であり、曖昧さを引き起こす可能性がありました | 明示的にされました |
[編集] 関連項目
| メモリ割り当て関数 (関数) |