我发现函数 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/