std::bad_array_new_length
From cppreference.com
| ヘッダ <new> で定義 |
||
| class bad_array_new_length; |
(C++11以降) | |
std::bad_array_new_length は、new式によって、無効な配列長を報告するために例外としてスローされるオブジェクトの型です。これは以下の場合に発生します。
- 配列長が負の場合。
- 新しい配列の総サイズが、実装定義の最大値を超える場合。
- 初期化子リストの数が初期化する要素の数を超える場合。
最初の配列次元のみがこの例外を生成する可能性があります。最初の次元以外の次元は定数式であり、コンパイル時にチェックされます。
継承図
目次 |
[編集] メンバ関数
| (コンストラクタ) |
新しいbad_array_new_lengthオブジェクトを構築します。(public member function) |
| operator= |
bad_array_new_lengthオブジェクトを置き換えます。(public member function) |
| what |
説明文字列を返す (public member function) |
std::bad_array_new_length::bad_array_new_length
| bad_array_new_length() noexcept; |
(1) | (C++11以降) |
| bad_array_new_length( const bad_array_new_length& other ) noexcept; |
(2) | (C++11以降) |
実装定義のヌル終端バイト文字列を持つ新しいbad_array_new_lengthオブジェクトを構築します。この文字列はwhat()を介してアクセスできます。
1) デフォルトコンストラクタ。
2) コピーコンストラクタ。
*thisとotherが両方とも動的型std::bad_array_new_lengthを持つ場合、代入後のstd::strcmp(what(), other.what()) == 0となります。パラメータ
| その他 | - | コピーする別の例外オブジェクト |
std::bad_array_new_length::operator=
| bad_array_new_length& operator=( const bad_array_new_length& other ) noexcept; |
(C++11以降) | |
otherの内容で内容を代入します。*thisとotherが両方とも動的型std::bad_array_new_lengthを持つ場合、代入後のstd::strcmp(what(), other.what()) == 0となります。
パラメータ
| その他 | - | 割り当てる別の例外オブジェクト |
戻り値
*this
std::bad_array_new_length::what
virtual const char* what() const noexcept; |
(C++11以降) (C++26 以降 constexpr) |
|
説明文字列を返します。
戻り値
説明情報を含む、実装定義のヌル終端文字列へのポインタ。この文字列は std::wstring として変換および表示するのに適しています。このポインタは、それが取得された例外オブジェクトが破棄されるか、例外オブジェクトの非 const メンバー関数(例:コピー代入演算子)が呼び出されるまで、少なくとも有効であることが保証されます。
|
返された文字列は、定数評価中に通常のリテラルエンコーディングでエンコードされます。 |
(C++26以降) |
注釈
実装は what() をオーバーライドすることが許可されていますが、必須ではありません。
std::bad_allocから継承
std::exception から継承
メンバ関数
| [virtual] |
例外オブジェクトを破棄する ( std::exception の仮想 public メンバー関数) |
| [virtual] |
説明文字列を返す ( std::exception の仮想 public メンバー関数) |
[編集] 注釈
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions |
202411L |
(C++26) | constexpr for exception types |
[編集] 例
std::bad_array_new_lengthがスローされるべき3つの条件
このコードを実行
#include <climits> #include <iostream> #include <new> int main() { try { int negative = -1; new int[negative]; } catch (const std::bad_array_new_length& e) { std::cout << "1) " << e.what() << ": negative size\n"; } try { int small = 1; new int[small]{1,2,3}; } catch (const std::bad_array_new_length& e) { std::cout << "2) " << e.what() << ": too many initializers\n"; } try { long large = LONG_MAX; new int[large][1000]; } catch (const std::bad_array_new_length& e) { std::cout << "3) " << e.what() << ": too large\n"; } std::cout << "End\n"; }
実行結果の例
1) std::bad_array_new_length: negative size 2) std::bad_array_new_length: too many initializers 3) std::bad_array_new_length: too large End
[編集] 関連項目
| メモリ割り当て関数 (関数) | |
| メモリ割り当てが失敗したときにスローされる例外 (クラス) |