std:: overflow_error
|
ヘッダーで定義
<stdexcept>
|
||
|
class
overflow_error
;
|
||
算術オーバーフローエラー(つまり、計算結果が宛先型に対して大きすぎる状況)を報告するために使用できる、例外としてスローされるオブジェクトの型を定義します。
|
この例外をスローする唯一の標準ライブラリコンポーネントは std::bitset::to_ulong です。 |
(C++11まで) |
|
この例外をスローする標準ライブラリコンポーネントは std::bitset::to_ulong と std::bitset::to_ullong です。 |
(C++11以降) |
標準ライブラリコンポーネントの数学関数はこの例外をスローしません(数学関数は
math_errhandling
で指定されている通りオーバーフローエラーを報告します)。しかし、サードパーティライブラリではこれを使用します。例えば、
boost.math
は
std::overflow_error
をスローします(
boost::math::policies::throw_on_error
が有効な場合、これがデフォルト設定です)。
std::overflow_errorのすべてのメンバー関数は
std::overflow_error
constexpr
です:定数式の評価中に
std::overflow_error
オブジェクトを作成して使用することが可能です。
ただし、
|
(C++26以降) |
継承図
目次 |
メンバー関数
|
(constructor)
|
指定されたメッセージで新しい
overflow_error
オブジェクトを構築する
(public member function) |
|
operator=
|
overflow_error
オブジェクトを置き換える
(public member function) |
std::overflow_error:: overflow_error
|
overflow_error
(
const
std::
string
&
what_arg
)
;
|
(1) | (constexpr since C++26) |
|
overflow_error
(
const
char
*
what_arg
)
;
|
(2) | (constexpr since C++26) |
|
overflow_error
(
const
overflow_error
&
other
)
;
|
(3) |
(noexcept since C++11)
(constexpr since C++26) |
std::overflow_error
を持つ場合、
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
となる。コピーコンストラクタから例外を送出することはできない。
パラメータ
| what_arg | - | 説明文字列 |
| other | - | コピーする別の例外オブジェクト |
例外
注記
std::overflow_error
のコピーは例外を送出することが許可されていないため、このメッセージは通常、別途割り当てられた参照カウント方式の文字列として内部的に格納される。これが
std::string&&
を受け取るコンストラクタが存在しない理由でもある:いずれにせよ内容をコピーする必要があるためである。
LWG issue 254 の解決以前は、非コピーコンストラクタは std::string のみを受け入れることができた。これにより、 std::string オブジェクトを構築するために動的割り当てが必須となっていた。
LWG issue 471
の解決後、派生した標準例外クラスは公開アクセス可能なコピーコンストラクタを持たなければならない。元のオブジェクトとコピーされたオブジェクトの
what()
によって得られる説明文字列が同じである限り、暗黙的に定義することができる。
std::overflow_error:: operator=
|
overflow_error
&
operator
=
(
const
overflow_error
&
other
)
;
|
(C++11以降noexcept)
(C++26以降constexpr) |
|
other
の内容を代入する。
*
this
と
other
の両方が動的型
std::overflow_error
を持つ場合、代入後は
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
となる。コピー代入演算子から例外が送出されることはない。
パラメータ
| other | - | 代入する別の例外オブジェクト |
戻り値
* this
注記
LWG issue 471
の解決後、派生標準例外クラスは公開アクセス可能なコピー代入演算子を持たなければならない。元のオブジェクトとコピーされたオブジェクトの
what()
によって得られる説明文字列が同じである限り、暗黙的に定義することができる。
std:: runtime_error から継承
std::exception から継承 std:: exception
メンバ関数
|
[virtual]
|
例外オブジェクトを破棄
(
std::exception
の仮想公開メンバ関数)
|
|
[virtual]
|
説明文字列を返す
(
std::exception
の仮想公開メンバ関数)
|
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions
|
202502L
|
(C++26) | constexpr 例外型 |
例
#include <iostream> #include <limits> #include <stdexcept> #include <utility> template<typename T, int N> requires (N > 0) /*...*/ class Stack { int top_{-1}; T data_[N]; public: [[nodiscard]] bool empty() const { return top_ == -1; } void push(T x) { if (top_ == N - 1) throw std::overflow_error("Stack overflow!"); data_[++top_] = std::move(x); } void pop() { if (empty()) throw std::underflow_error("Stack underflow!"); --top_; } T const& top() const { if (empty()) throw std::overflow_error("Stack is empty!"); return data_[top_]; } }; int main() { Stack<int, 4> st; try { [[maybe_unused]] auto x = st.top(); } catch (std::overflow_error const& ex) { std::cout << "1) Exception: " << ex.what() << '\n'; } st.push(1337); while (!st.empty()) st.pop(); try { st.pop(); } catch (std::underflow_error const& ex) { std::cout << "2) Exception: " << ex.what() << '\n'; } try { for (int i{}; i != 13; ++i) st.push(i); } catch (std::overflow_error const& ex) { std::cout << "3) Exception: " << ex.what() << '\n'; } }
出力:
1) Exception: Stack is empty! 2) Exception: Stack underflow! 3) Exception: Stack overflow!
欠陥報告
以下の動作変更欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 254 | C++98 | const char * を受け取るコンストラクタが欠如していた | 追加された |
| LWG 471 | C++98 |
std::overflow_error
のコピーの説明文字列が
実装定義であった |
元の
std::overflow_error
オブジェクトの
説明文字列と同じである |