Lately I've been converting some libraries to use the <system_error>
facilities in C++11.
I'm having difficulty understanding the use cases for std::error_code
vs. std::error_condition
.
Note, I understand the difference - there are many questions on stackoverflow which go over the difference.
The basic difference is that std::error_code
is supposed to represent a system- or platform-specific error, whereas std::error_condition
is an abstract error that an API or user interface should return.
Okay - but I'm having trouble understanding why we would ever use std::error_code
in practice. It seems to me you're either going to :
Be dealing with a system specific error reporting mechanism (like say,
errno
or something returned from a POSIX call, or say, a call togetsockopt
withSO_ERROR
on Linux) which you can easily convert to anstd::error_condition
via thestd::errc
enums, which are supposed to be portable.Be using a user-defined category of errors, which represent application-level or business-logic errors, like "invalid social security number" or whatever - which also would be a use case for
std::error_condition
.Be dealing with some low-level interface or library which defines its own error reporting mechanism, such as OpenSSL, in which case you would be directly using platform-specific error mechanisms. In this case you'd then need to convert or map these errors to an
std::error_code
. But if you're going to go through the trouble of converting these platform specific errors to something generic likestd::error_code
, why not just convert tostd::error_condition
?
Also, since POSIX system errors are supposed to be portable, and since they map one-to-one with std::error_condition
via the std::errc
enum, I can't find any use case for std::error_code
. Most Linux/UNIX system calls set errno
, which is supposed to portably map to std::error_condition
.
So, I don't see any use case for std::error_code
anywhere. So, what are some example use cases where we would want to use std::error_code
instead of std::error_condition
?