std::length_error
在标头
<stdexcept>
定义
|
||
class
length_error;
|
||
定义作为异常抛出的对象类型。它报告试图超出一些对象的实现定义长度极限所导致的错误。
std::basic_string 的成员函数和 std::vector::reserve 会抛出此异常。
继承图
成员函数
(构造函数)
|
构造拥有给定消息的新 length_error 对象 (公开成员函数) |
operator=
|
替换 length_error 对象 (公开成员函数) |
std::length_error::length_error
length_error(
const
std::string&
what_arg );
|
(1) | |
length_error(
const
char*
what_arg );
|
(2) | |
(3) | ||
length_error(
const
length_error&
other );
|
(C++11 前) | |
length_error(
const
length_error&
other )
noexcept;
|
(C++11 起) | |
std::length_error
,那么 std::strcmp(what(), other.what()) == 0。复制构造函数不能抛出异常。
(C++11
前)参数
what_arg | - | 解释字符串 |
other | - | 要复制的另一异常对象 |
异常
注解
因为不容许复制 std::length_error
抛出异常,通常将此消息在内部存储为分离分配的引用计数字符串。这也是构造函数不接收
std::string&&
参数的理由:无论如何它必须复制内容。
在解决 LWG 问题 254 之前,非复制的构造函数只接受 std::string。这导致因需要构造 std::string 对象而不得不进行动态内存分配。
在解决 LWG 问题
471
之后,派生的标准异常类必须有一个公开可访问的复制构造函数。它可以隐式定义,只要分别在原对象和复制对象上通过
what()
获得的两个解释字符串相同。
std::length_error::operator=
length_error&
operator=(
const
length_error&
other );
|
(C++11 前) | |
length_error&
operator=(
const
length_error&
other )
noexcept;
|
(C++11 起) | |
将 other
的内容用于赋值。 如果 *this 与
other
的动态类型都是 std::length_error
,那么在赋值后 std::strcmp(what(), other.what()) == 0。复制赋值运算符不能抛出异常。
(C++11
前)
Parameters
other | - | 要赋值来的另一异常对象 |
Return value
*this
Notes
在解决 LWG 问题
471
之后,派生的标准异常类必须有一个公开可访问的复制赋值运算符。它可以隐式定义,只要分别在原对象和复制对象上通过
what()
获得的两个解释字符串相同。
继承自 std::logic_error
继承自 std::exception
成员函数
[虚]
|
析构该异常对象 ( std::exception
的虚公开成员函数) |
[虚]
|
返回解释性字符串 ( std::exception
的虚公开成员函数) |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 254 | C++98 | 缺失了接受 const char* 的构造函数 | 已补充 |
LWG 471 | C++98 | std::length_error 的复制的解释字符串由实现定义
|
它们与原 std::length_error 对象的解释字符串相同
|
参阅
更改存储的字符数 ( std::basic_string<CharT,Traits,Allocator>
的公开成员函数) |