c++ - 错误代码与错误条件

标签 c++ c++11

我不太明白为什么我们需要区分错误代码 (std::error_code) 和错误 condition(std::error_condition) ,他们不是一回事吗?错误条件与错误代码的优势是什么?

最佳答案

来自 http://en.cppreference.com/w/cpp/error/error_condition

std::error_condition is a platform-independent error code. Like std::error_code, it is uniquely identified by an integer value and a std::error_category, but unlike std::error_code, the value is not platform-dependent.

因此,使用 std::error:condition 时,您的错误代码并不特定于您正在处理的平台。

带有 std::error_code

Each std::error_code object holds a pair of error code originating from the operating system, or some low-level interface

因此,error_code 将引用特定于您的平台、硬件等的内容。

同时使用两者可能是有利的。 error_condition 是“可移植的抽象”,因此将是提供给用户的通用错误消息,而 error_code 将是对特定调试有用的平台相关信息.

A typical implementation [of error_condition] holds one integer data member (the value) and a pointer to an std::error_category.

关于c++ - 错误代码与错误条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16687403/

相关文章:

c++ - std::transform 到任意容器

c++ - OpenCV : findcontours() , 外部轮廓太多

c++ - CRTP - 静态接口(interface)中的 "abstract"方法

c++ - 将 chrono::milliseconds 转换为 uint64_t?

c++ - 缺陷报告 1207

c++ - 非依赖名称查找和 lambda

c++ - 如何在 C++ 中对具有十六进制值的字符串执行按字节异或?

C++ string += operator 内存结果

c++ - 什么样的值是模板参数?我能(不能)用它们做什么?

c++ - 运算符 '...' 具有覆盖说明符但不覆盖基类成员