std::system_category
来自 cppreference.cn
定义于头文件 <system_error> |
||
const std::error_category& system_category() noexcept; |
(自 C++11 起) | |
获取对静态错误类别对象的引用,该对象用于操作系统报告的错误。该对象需要重写虚函数 std::error_category::name() 以返回指向字符串 "system" 的指针。它还需要重写虚函数 std::error_category::default_error_condition(),以将与 POSIX errno 值匹配的错误码映射到 std::generic_category。
内容 |
[编辑] 参数
(无)
[编辑] 返回值
对未指定运行时类型的静态对象的引用,该对象派生自 std::error_category。
[编辑] 注释
在 Windows 上,system_category()
通常将一些 Windows 错误码 映射到 POSIX 错误码。在 POSIX 上,system_category()
往往等同于 std::generic_category(),除了名称。
[编辑] 示例
运行此代码
#include <iomanip> #include <iostream> #include <string> #include <system_error> int main() { for (int const code : {EDOM, 10001}) { const std::error_condition econd = std::system_category().default_error_condition(code); std::cout << "Category: " << econd.category().name() << '\n' << "Value: " << econd.value() << '\n' << "Message: " << econd.message() << "\n\n"; } }
可能的输出
Category: generic Value: 33 Message: Numerical argument out of domain Category: system Value: 10001 Message: Unknown error 10001
[编辑] 参见
(C++11) |
标识通用错误类别 (函数) |
(C++11) |
std::error_condition 枚举,列出所有标准的 <cerrno> 宏常量 (类) |