我应该在Unix上使用system_category或generic_category for errno吗? [英] Should I use system_category or generic_category for errno on Unix?

查看:246
本文介绍了我应该在Unix上使用system_category或generic_category for errno吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 0x有两个预定义的 error_category 对象: generic_category() system_category ()。根据我到目前为止的理解, system_category()应用于操作系统返回的错误, generic_category()应该用于 std :: errc 中找到的泛型值,它们对应于 errno 值。 p>

但是,在类似Unix的系统上应该做什么,其中 errno 操作系统返回的错误?我应该使用 system_category()(这将是错误的非类Unix系统,需要 #ifdef ) ,或者应该使用 generic_category()(对于非标准的 errno 值)

解决方案

您应该报告错误从操作系统(任何一个,包括基于POSIX的操作系统, code> system_category() as C ++ standard library functions do - see the quote from C ++ 11 standard below:


17.6.5.14错误代码的值[value.error.codes]



1 C ++标准库中的某些函数通过std :: error_code (19.5.2.1)对象。
对象的category()成员应返回std :: system_category(),以查找源于操作
系统
的错误,或对实现定义的error_category对象的引用
。实现应为这些错误类别中的每一个定义value()的可能值。
[示例:对于基于POSIX的操作系统,鼓励实现将
std :: system_category()值定义为与POSIX errno值相同,并定义附加值
通过操作系统的文档。鼓励在POSIX上不基于
的操作系统的实现定义与操作系统的值相同的值。对于
不是来源于操作系统的错误,实现可以为相关联的值提供枚举。
-end example]



C++0x has two predefined error_category objects: generic_category() and system_category(). From what I have understood so far, system_category() should be used for errors returned by the operating system, and generic_category() should be used for the generic values found in std::errc, which correspond to errno values.

However, what should be done on Unix-like systems, where errno values are the errors returned by the operating system? Should I use system_category() (which would be wrong on non-Unix-like systems, needing an #ifdef), or should I use generic_category() (which would be wrong on Unix-like systems for non-standard errno values)?

解决方案

You are meant to report errors from OS (any one, including POSIX-based OSes such as Unix) using system_category() as C++ standard library functions do - see the quote from C++11 standard below:

17.6.5.14 Value of error codes [value.error.codes]

1 Certain functions in the C++ standard library report errors via a std::error_code (19.5.2.1) object. That object’s category() member shall return std::system_category() for errors originating from the operating system, or a reference to an implementation-defined error_category object for errors originating elsewhere. The implementation shall define the possible values of value() for each of these error categories. [ Example: For operating systems that are based on POSIX, implementations are encouraged to define the std::system_category() values as identical to the POSIX errno values, with additional values as defined by the operating system’s documentation. Implementations for operating systems that are not based on POSIX are encouraged to define values identical to the operating system’s values. For errors that do not originate from the operating system, the implementation may provide enums for the associated values. —end example ]

这篇关于我应该在Unix上使用system_category或generic_category for errno吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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