Should I use system_category or generic_category for errno on Unix? Should I use system_category or generic_category for errno on Unix? unix unix

Should I use system_category or generic_category for errno on Unix?


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. Thatobject’s category() member shall return std::system_category() for errors originating from the operatingsystem, or a reference to an implementation-defined error_category object for errors originatingelsewhere. 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 thestd::system_category() values as identical to the POSIX errno values, with additional values as definedby the operating system’s documentation. Implementations for operating systems that are not basedon POSIX are encouraged to define values identical to the operating system’s values. For errors that donot originate from the operating system, the implementation may provide enums for the associated values.—end example ]


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 from system_category be the same as generic_category.