std::error_condition

来自cppreference.com
< cpp‎ | error
 
 
 
 
 
在标头 <system_error> 定义
class error_condition;
(C++11 起)

std::error_condition 持有用于指定错误条件的独立于平台的值。类似于 std::error_code,它由一个整数值和 std::error_category 所唯一标识,但不同于 std::error_code,该值不依赖平台。

典型的实现保有整数数据成员(其值)和一个指向 std::error_category 的指针。

目录

[编辑] 成员函数

构造 error_condition
(公开成员函数) [编辑]
替换内容
(公开成员函数) [编辑]
替���内容
(公开成员函数) [编辑]
设置 error_conditiongeneric_category 中的值 0
(公开成员函数) [编辑]
获得 error_condition 的值
(公开成员函数) [编辑]
获得此 error_conditionerror_category
(公开成员函数) [编辑]
获得解释性字符串
(公开成员函数) [编辑]
检查值是否非零
(公开成员函数) [编辑]

[编辑] 非成员函数

(C++20 移除)(C++20 移除)(C++20)
比较 error_conditionerror_code
(函数) [编辑]

[编辑] 辅助类

鉴别枚举是否可作为 std::error_condition
(类模板) [编辑]
std::error_condition 的散列支持
(类模板特化) [编辑]

[编辑] 注解

std::error_codestd::error_condition 之间的比较是由它们的错误类别定义的。特别是,std::generic_category 的某个错误条件,与某个特定类别(比如 std::system_category)的某个错误码,如果它们代表相同种类的错误,则可以比较相等。

std::errc 值可以通过向 std::error_condition 的隐式转换来进行与错误码的比较。

#include <cerrno>
#include <iostream>
#include <system_error>
#include <Windows.h>
 
int main()
{
    std::error_code ec{ERROR_FILE_EXISTS, std::system_category()};
    std::error_condition econd{EEXIST, std::generic_category()};
 
    std::cout.setf(std::ios::boolalpha);
    std::cout << (ec == econd) << '\n'; // 通常为 true
    std::cout << (ec == std::errc::file_exists) << '\n'; // 同上
    std::cout << (ec == make_error_code(std::errc::file_exists)) << '\n'; // false:
                                                                     // 不同类别
}

可能的输出:

true
true
false

[编辑] 参阅

保有依赖于平台的错误码
(类) [编辑]
错误类别的基类
(类) [编辑]
errce 创建错误条件
(函数) [编辑]