如何将std :: system_error异常与std :: errc值进行可移植的比较? [英] How to portably compare std::system_error exceptions to std::errc values?

查看:84
本文介绍了如何将std :: system_error异常与std :: errc值进行可移植的比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,以可移植的方式检查 system_error 条件的最佳实践之一是将它们的 code()值与 std :: errc 枚举.但是,当我尝试运行以下代码时,这似乎不起作用.

As far as I've understood, one of the best practices to check system_error conditions in a portable manner is to compare their code() value with values in the std::errc enumeration. However, when I try to run the following code, this does not seem to work.

#include <cassert>
#include <cerrno>
#include <system_error>

int main() {
    try {
        throw std::system_error(ENOENT, std::system_category());
    } catch (std::system_error const & e) {
        assert(e.code() == std::errc::no_such_file_or_directory); // <- FAILS!?
    }
}

我是否误解了这些诊断错误的工作方式,还是我做错了什么?

Do I misunderstand how these diagnostic errors are supposed to work, or am I doing something wrong? How should one compare std::system_error exceptions to std::errc values?

该代码似乎可以使用 clang ++ 和libc ++正常运行,但是无论我使用哪种GCC或Clang编译器(和版本),都无法针对libstdc ++构建代码.与 PR 60555 相关吗?有便携式解决方案吗?

The code seems to work properly using clang++ and libc++, but fails when building against libstdc++ regardless of which GCC or Clang compiler (and version) I use. Related to PR 60555? Any portable workaround?

推荐答案

您没有做错任何事情.正如T.C.的评论所证实的那样并最近 类似 问题,这确实是由

You're not doing anything wrong. As confirmed in comments by T.C. and in recent similar questions, this is indeed caused by PR #60555. Fortunately, this bug was already fixed in their VCS by August 8, 2018:

已修复在所有活动分支上,因此将在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开发人员发布新版本的GCC的问题,而且要花几年的时间才能将它们合并到流行的发行版中,直到我们最终可以开始使用此出色的现代功能为止C ++ 11.但这就是生活...

There seems to be no good workaround for this, so it is now just a matter of GCC developers releasing new versions of GCC and a matter of years until these get incorporated into popular distributions until we can finally start using this nice modern feature of C++11. But that's life...

这篇关于如何将std :: system_error异常与std :: errc值进行可移植的比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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