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
在启用
boost::math::policies::throw_on_error
(默认设置)时会抛出
std::overflow_error
。
std::overflow_error
的所有成员函数均为
constexpr
:可以在常量表达式求值过程中创建和使用
std::overflow_error
对象。
然而,
|
(since C++26) |
继承关系图
目录 |
成员函数
|
(constructor)
|
使用给定消息构造新的
overflow_error
对象
(公开成员函数) |
|
operator=
|
替换
overflow_error
对象
(公开成员函数) |
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 问题 471
解决后,派生的标准异常类必须具有可公开访问的复制赋值运算符。只要原始对象和复制对象通过
what()
获得的说明字符串相同,该运算符就可以隐式定义。
继承自 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++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 254 | C++98 | 缺少接受 const char * 的构造函数 | 已添加 |
| LWG 471 | C++98 |
std::overflow_error
副本的说明字符串
由实现定义 |
与原始
std::overflow_error
对象
的说明字符串相同 |