error_code:如何设置和检查错误
error_code: how to set and check errno
我试图了解在 Linux 上调用设置errno
的 C 函数时应该使用哪个类别。
我不确定 POSIX 定义了所有可能的错误代码,所以我很想使用system_category
.
但是我喜欢稍后在我的代码中处理通用条件,所以我想做这样的事情:
std::error_code ec;
some_func(some_path, ec);
if (ec) {
if (ec == std::errc::file_exists) {
// special handling
}
return ec;
}
要在some_func()
中设置错误代码,我希望这样做:
ec.assign(EEXIST, std::system_category());
主要基于此讨论:
类别和标准/系统错误代码 - 以及@niall道格拉斯提供的代码示例:
std::error_code ec; if(-1 == open(...)) ec = std::error_code(errno, std::system_category()); // To test using portable code if(ec == std::errc::no_such_file_or_directory) ... // To convert into nearest portable error condition (lossy, may fail) std::error_condition ec2(ec.default_error_condition())
-- https://stackoverflow.com/a/40063005/951426
但是,在Linux上,使用GCC 6.1.1,我有:
std::error_code(EEXIST, std::system_category()) == std::errc::file_exists
返回false
std::error_code(EEXIST, std::generic_category()) == std::errc::file_exists
返回true
我期望errno + system_category与std::errc
条件相当。
这意味着如果我不使用泛型类别,我检查if (ec == std::errc::file_exists)
的初始代码将不起作用。
这是预期的行为吗?
这是最近在最新的 GCC 6、7 和 8 点版本中修复的错误。如果您使用的是最新的单点版本,它将按预期工作。请参阅 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60555。
相关文章:
- error_code:如何设置和检查错误
- 我可以检查初始化列表中设置的构造函数主体中的变量吗
- 检查是否至少设置了一点而不跳跃
- 如何检查 int 中是否恰好设置了一位?
- 哪种检查方法比设置值最理想
- 检查是否已设置 glm::vec3
- 检查是否为输入流设置了Skipws/noskipws标志
- 在编译时间检查模板参数类型是否设置或多键,并且容器的元素类型是算术的
- 检查Linux功能以设置线程优先级
- 如何在广义inttype上设置/检查msb
- 标志-检查是否设置了位并且仅设置了这些位
- 如何检查指针数组中的 c++ 对象指针是否已设置
- 检查是否在整数变量中设置了标志
- 在C++中设置变量的值之前检查变量是否更好
- 首次机会异常设置检查列表视图中的状态
- OpenCV:检查cv::Point是否已设置值
- 设置makefile以使用emacs flymake检查c和c++源文件
- 如何检查为给定呼叫站点设置的过载解决方案
- c++ 设置插入编号以检查重复项
- 如何在菜单项上设置检查