namespace std {
class length_error : public logic_error;
}
概要
length_errorクラスは、オブジェクトの最大許容サイズを超える長さのオブジェクトを生成しようとしたことを通知するために送出される例外クラスである。
std::basic_stringやstd::vectorなどのコンテナで、max_size()を超える要素数を要求した場合などに送出される。
このクラスはlogic_errorを公開継承しており、logic_errorとして捕捉できる。
メンバ関数
| 名前 | 説明 | 対応バージョン |
|---|---|---|
(constructor) |
コンストラクタ | |
(destructor) |
デストラクタ | |
operator= |
代入演算子 | |
what |
エラー理由を取得する(std::exceptionから継承) |
例
#include <stdexcept>
#include <iostream>
#include <string>
int main()
{
try {
std::string s;
// 最大長を超える長さを要求する
s.resize(s.max_size() + 1);
}
catch (const std::length_error& e) {
std::cout << "length_error: " << e.what() << std::endl;
}
}
出力例
length_error: basic_string::_M_replace_aux
例外オブジェクトが保持するメッセージの内容は、処理系によって異なる。
関連項目
参照
- P3378R2
constexprexception types- C++26で
constexpr対応した
- C++26で