std::format_error
来自 cppreference.cn
定义于头文件 <format> |
||
class format_error; |
(C++20 起) | |
定义用于报告格式化库错误的异常对象类型。
继承图
目录 |
[编辑] 成员函数
(构造函数) |
使用给定消息构造一个新的 format_error 对象(公开成员函数) |
operator= |
替换 format_error 对象(公开成员函数) |
std::format_error::format_error
format_error( const std::string& what_arg ); |
(1) | |
format_error( const char* what_arg ); |
(2) | |
format_error( const format_error& other ) noexcept; |
(3) | |
3) 复制构造函数。如果 *this 和 other 都具有动态类型
std::format_error
,则 std::strcmp(what(), other.what()) == 0。复制构造函数不能抛出异常。参数
what_arg | - | 解释性字符串 |
其他 | - | 要拷贝的另一个异常对象 |
异常
1,2) 可能抛出 std::bad_alloc。
注意
因为不允许 std::format_error
的复制操作抛出异常,所以此消息通常在内部以单独分配的引用计数字符串存储。这也是为什么没有接受 std::string&&
的构造函数:它无论如何都必须复制内容。
派生标准异常类必须具有公共可访问的复制构造函数。只要原始对象和复制对象通过 what()
获取的解释性字符串相同,就可以隐式定义它。
std::format_error::operator=
format_error& operator=( const format_error& other ) noexcept; |
||
用 other 的内容赋值。如果 *this 和 other 都具有动态类型 std::format_error
,则赋值后 std::strcmp(what(), other.what()) == 0。复制赋值运算符不能抛出异常。
参数
其他 | - | 用于赋值的另一个异常对象 |
返回值
*this
注意
派生标准异常类必须具有公共可访问的复制赋值运算符。只要原始对象和复制对象通过 what()
获取的解释性字符串相同,就可以隐式定义它。
继承自 std::exception
成员函数
[virtual] |
销毁异常对象 ( std::exception 的虚公共成员函数) |
[virtual] |
返回解释字符串 ( std::exception 的虚公共成员函数) |
[编辑] 示例
运行此代码
#include <format> #include <print> #include <string_view> #include <utility> int main() { try { auto x13{37}; auto args{std::make_format_args(x13)}; std::ignore = std::vformat("{:()}", args); // throws } catch(const std::format_error& ex) { std::println("{}", ex.what()); } }
可能的输出
format error: failed to parse format-spec