在 Unix 上,我应该对 errno 使用 system_category 还是 generic_category ?
C++0x 有两个预定义的 error_category
对象:generic_category()
和 system_category()
。据我所知,到目前为止, system_category()
应该用于操作系统返回的错误, generic_category()
应该用于 generic_category()
中找到的通用值code>std::errc,对应于errno
值。
但是,在类 Unix 系统上应该做什么,其中 errno
值是操作系统返回的错误?我应该使用 system_category()
(这在非类 Unix 系统上是错误的,需要 #ifdef
),还是应该使用 generic_category()< /code> (对于非标准
errno
值,在类 Unix 系统上这会是错误的)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该像 C++ 标准库函数一样使用
system_category()
报告操作系统(任何操作系统,包括基于 POSIX 的操作系统,例如 Unix)的错误 - 请参阅下面的 C++11 标准引用: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:除非您实际上是操作系统(或报告操作系统特定功能的错误),否则不应使用
system_category
。类别描述了错误的来源,而不一定是错误代码的含义。因此,system_category
中的可能错误代码集与generic_category
相同是完全合法的。You should not use
system_category
unless you are in fact the operating system (or reporting an error from an OS-specific function). The category describes where the error originates from, not necessarily what the error code means. So it is perfectly legitimate to have the set of possible error codes fromsystem_category
be the same asgeneric_category
.