std::nothrow
ja.cppreference.net より
| ヘッダ <new>
|
||
struct nothrow_t {};
|
で定義されます。 (1) | (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 | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2510 | C++11 | デフォルトコンストラクタが非explicitであり、曖昧性を引き起こす可能性があった | explicitに変更 |
関連項目
|
確保関数
(関数) |