C 0x有两个预定义的error_category对象:generic_category()和system_category().从我到目前为止所理解的,system_category()应该用于操作系统返回的错误,generic_category()应该用于std :: errc中的通用值,它们对应于errno值.
但是,在类Unix系统上应该做些什么,其中errno值是操作系统返回的错误?我应该使用system_category()(在非类Unix系统上会出错,需要#ifdef),还是应该使用generic_category()(对于非标准的errno值,在类Unix系统上会出错)?
解决方法
您应该使用system_category()报告来自OS(任何一个,包括基于POSIX的操作系统,如Unix)的错误,如C标准库函数所做的那样 – 请参阅下面C 11标准的引用:
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 ]
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|