错误处理。将系统错误代码映射到通用 [英] Error handling. Mapping system error codes to generic

查看:237
本文介绍了错误处理。将系统错误代码映射到通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现函数 default_error_condition 不能按我的代码预期的那样工作

I've found that function default_error_condition doesn't work as expected in my code

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

返回的 ec 值具有 system 错误类别,但具有如果我从文档中得到正确的答案,请成为通用 cppreference 和gcc源代码 system_error.cc

Returned ec value has system error category, but it has to be generic, if I got it right from the documentation e.g. cppreference and gcc source code system_error.cc

UPD:也在标准 19.5.1.5错误类别对象


对象的default_error_condition中也找到了此注释虚拟函数的行为如下:

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

如果参数ev对应于POSIX errno值posv,则函数应返回error_condition(posv,generic_category())。否则,该函数应返回error_condition(ev,system_category())

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())

这是怎么回事?

I在Linux上使用g ++ 7.3.0

I'm using g++ 7.3.0 on linux

推荐答案

由于参数的原因,您说对了, error_condition(ev,generic_category())应该从 default_error_condition 返回,因此输出应为 generic

You're right that, because of the argument, an error_condition(ev,generic_category()) should be returned from default_error_condition and thus the output should be "generic".

查看您链接到的源的历史记录,直到最近(直到三个月前)。这是错误60555

Looking at the "history" of the source you linked to, this was a libstdc++ bug until very recently (just three months ago). It was bug 60555.

乔纳森的结论是:


已修复所有活动分支,因此将在6.5、7.4、8.3中修复和9.1版本。

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(尚不存在),则d查看预期的行为。

So, if you were to upgrade your GCC 7.3 to 7.4 (which doesn't exist yet), you'd see the expected behaviour.

这篇关于错误处理。将系统错误代码映射到通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆