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?
Aucun commentaire:
Enregistrer un commentaire