std::length_error
来自 cppreference.cn
定义于头文件 <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) | |
length_error( const length_error& other ); |
(3) | (C++11 起无异常抛出) |
3) 拷贝构造函数。如果 *this 和 other 都具有动态类型
std::length_error
,则 std::strcmp(what(), other.what()) == 0。拷贝构造函数不能抛出异常。参数
what_arg | - | 解释性字符串 |
其他 | - | 要拷贝的另一个异常对象 |
异常
1,2) 可能抛出 std::bad_alloc。
注意
因为不允许拷贝 std::length_error
抛出异常,所以此消息通常在内部存储为单独分配的引用计数字符串。这也是为什么没有接受 std::string&&
的构造函数:无论如何它都必须拷贝内容。
在 LWG issue 254 解决之前,非拷贝构造函数只能接受 std::string。它使动态分配成为强制性,以便构造 std::string 对象。
在 LWG issue 471 解决之后,派生的标准异常类必须具有公开可访问的拷贝构造函数。只要原始对象和拷贝对象的 what()
获取的解释性字符串相同,它就可以隐式定义。
std::length_error::operator=
length_error& operator=( const length_error& other ); |
(C++11 起无异常抛出) | |
将内容赋值为 other 的内容。如果赋值后 *this 和 other 都具有动态类型 std::length_error
,则 std::strcmp(what(), other.what()) == 0。拷贝赋值操作符不能抛出异常。
参数
其他 | - | 用于赋值的另一个异常对象 |
返回值
*this
注意
在 LWG issue 471 解决之后,派生的标准异常类必须具有公开可访问的拷贝赋值操作符。只要原始对象和拷贝对象的 what()
获取的解释性字符串相同,它就可以隐式定义。
继承自 std::logic_error
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 254 | C++98 | 缺少接受 const char* 的构造函数 | 已添加 |
LWG 471 | C++98 | std::length_error 的解释性字符串解释性字符串是实现定义的 |
它们与原始 std::runtime_error 对象的原始 std::length_error 对象 |
[编辑] 另请参阅
更改存储的字符数 ( std::basic_string<CharT,Traits,Allocator> 的公共成员函数) |