C++11 introduced the <system_error>
header containing a generic system to handle error codes. An std::error_code
is a tuple containing an int
, the error code, and a reference to an std::error_category
, which defines the error domain and handling of the error code. The standard library comes with four categories: std::generic_category
, std::system_category
, std::future_category
, and std::iostream_category
.
There are conflicts on which category to use, both here on SO and on C++ reference sites, when creating std::error_code
s/throwing std::system_error
s with errno
and WinAPI error codes:
errno
withstd::generic_category
: SO answer, llvm-commits, cplusplus.comerrno
withstd::system_category
: SO answer, cppreference.comGetLastError()
withstd::generic_category
: SO answerGetLastError()
withstd::system_category
: SO answer, SO comment
However, errno
and GetLastError()
can't use the same category, otherwise some error codes would be ambiguous. Error code 33 is one example, as it is both EDOM
and ERROR_LOCK_VIOLATION
.
There are even some places advocating a user-made category for the WinAPI, but I can't find any references to that at the moment. This alternative would be specially painful.
Which category should be used with errno
, and which should be used with GetLastError()
so that
std::error_code::default_error_condition()
std::error_code::message()
are unambinguous and appropriate to the underlying error code?
In the C++ standard:
system_category
The current C++17 draft states that:
It's not so clear:
what is supposed to happen to
errno
values on Windows?is an
errno
from a POSIX call "originating from the operating system" or is this supposed to be restricted to non POSIX calls?generic_category
std::errc
is an enumeration with the same values as the C/POSIXEFOOBAR
errors code;make_error_code(std::errc)
generates anerro_code
usinggeneric_category
This means that POSIX error code can be used with
generic_category
. Non POSIX values might possibly not work correctly withgeneric_catgeory
. In practice, they seem to be supported by the implementations I've been using.In Boost
Boost system itself
The Boost documentation is quite terse about this feature:
Moreover you can find legacy declaration such as:
In
linux_error.hpp
:In
windows_error.hpp
:In
cygwin_error.hpp
:For Windows, Boost uses
system_category
for nonerrno
errors:In ASIO
We find this kind of code in ASIO:
We find
errno
assystem_category
in POSIX code:Filesystem
We find
errno
withgeneric_category
in POSIX code:In GNU libstdc++
Filesystem
We find
errno
withgeneric_category
:and no usage of
system_category
.Using libstdc++
In practice, it seems you can use
generic_category
for non-POSIXerrno
with libstdc++:Gives:
Libc++
We find
errno
withsystem_category
:but no usage of
generic_category
.Conclusion
I don't find any consistent pattern here but apparently:
you are expected to use
system_category
when using Windows error on Windows;you can safely use
generic_category
for POSIX values oferrno
;you are not supposed to be able to use
std::generic_category
for non-POSIX vales oferrno
(it might not work);If you do not want to check if your
errno
value is a POSIX one:on POSIX-based systems you are expected to be able to useon POSIX-based systems you can usesystem_error
witherrno
(strictly speaking the support for this is not mandated, only encouraged).system_error
witherrno
.I have to admit to a bit of surprise at the confusion regarding <system_error> given Chris summarised exactly how it works at http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-1.html and I personally find the C++ standard text above perfectly clear. But to summarise in very succinct words:
If on POSIX:
generic_category
=> POSIX standard errno spacesystem_category
=> Local POSIX errno space (usually extends POSIX with proprietary errno codes). Usestrerror()
to expand codes into string descriptions returned bymessage()
.In practice on POSIX both implementations are the same underneath and map the native errno space.
If on Windows:
generic_category
=> POSIX standard errno space which is returned by various POSIX emulation functions in the MSVCRT likefopen()
etcsystem_category
=> The Win32GetLastError()
space. UseFormatMessage()
to expand codes into string descriptions returned bymessage()
.How to use <system_error> portably
Other thoughts:
Some commentators have said that <system_error> is poorly designed and shouldn't be used. This is simply not true, it's pretty optimal given the C++ 03 idiomatic practice of the time of its design, it generates very tight high quality fixed latency code on all major STLs except Dinkumware's. It's user extensible to any arbitrary error code system, and standardises unifying into a single system disparate third party library error handling.
It is true it would look quite different today had constexpr global variables been available at the time of its design, and maybe that might get rectified in a C++ standard coming after 17. But if you are a programmer who needs to move around error codes from third party libraries without losing information through code not written to know about those third party libraries, then <system_error> is an excellent solution.
Consider it as similar to the
virtual
keyword for third party library error code handling - it erases the need for code transporting third party codes from needing to understand those codes. If you have that problem in your code base - and most large code bases do - then absolutely you should be using <system_error> instead of whatever error code mapping or translation system you're currently using.