c++ - 错误处理。将系统错误代码映射到通用错误代码

标签 c++ error-handling g++ std

我发现函数 default_error_condition 在我的代码中无法按预期工作

auto ec = std::system_category().default_error_condition(EACCES);
std::cout << ec.value() << std::endl << ec.category().name() << std::endl;

返回的ec值具有系统错误类别,但它必须是通用,如果我从文档中得到正确的结果,例如cppreference和gcc源代码system_error.cc

UPD:在标准19.5.1.5错误类别对象中也发现了此注释

The object’s default_error_condition virtual function shall behave as follows:

If the argument ev corresponds to a POSIX errno value posv, the function shall return error_condition(posv, generic_category()). Otherwise, the function shall return error_condition(ev, system_category())

这里发生了什么?

我在 Linux 上使用 g++ 7.3.0

最佳答案

你是对的,由于参数的原因,应该从 default_error_condition 返回 error_condition(ev,generic_category()) ,因此输出应该是 “通用”

查看您链接到的源的“历史”,直到最近,这都是一个 libstdc++ 错误(只是 three months ago )。这是bug 60555 .

乔纳森的结论是:

Fixed on all active branches, so will be fixed in the 6.5, 7.4, 8.3 and 9.1 releases.

因此,如果您要将 GCC 7.3 升级到 7.4(尚不存在),您会看到预期的行为。

关于c++ - 错误处理。将系统错误代码映射到通用错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105183/

相关文章:

php - 如何 try catch 并继续php中的脚本

c++ - cc1plus : error: unrecognized command line option "-fsysroot=<path>" with g++

c++ - GLOBAL__I_main 函数

c++ 指针参数 : does it make sense to create a copy of the value pointed too, 然后再次将拷贝存储为指针?

C# 无法在未损坏的 C++ 库上找到入口点

c++ - 循环中列表的最后一个元素

ios - “SDWebImageCompat.h”文件未找到错误

c++ - 我必须实现哪些功能才能使类可迭代?

error-handling - coldfusion 10-自定义处理时隐藏状态码

c++ - C++ 程序执行错误 : static executable calls DLL library