I don't quite get why do we need to make a distinction between error code (std::error_code)
and an error condition(std::error_condition)
, aren't they the same thing? What are the advantages of an error condition vs error code?

- 57,103
- 20
- 141
- 208

- 829
- 9
- 11
3 Answers
From http://en.cppreference.com/w/cpp/error/error_condition
std::error_condition
is a platform-independent error code. Likestd::error_code
, it is uniquely identified by an integer value and astd::error_category
, but unlikestd::error_code
, the value is not platform-dependent.
So, the advantage is your error code isn't specific to the platform you're working on when using std::error_condition
.
With an std::error_code
Each
std::error_code
object holds a pair of error code originating from the operating system, or some low-level interface
So, the error_code
will reference something specific to your platform, a piece of hardware etc etc.
It may be advantageous to use both. The error_condition
is the "portable abstraction" so would be the generic error message to give to the user and the error_code
would be the platform dependent information that would be useful for specific debug.
A typical implementation [of
error_condition
] holds one integer data member (the value) and a pointer to anstd::error_category
.
The simplest answer to this question I found here: http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-5.html.
- class
std::error_code
- represents a specific error value returned by an operation (such as a system call).- class
std::error_condition
- something that you want to test for and, potentially, react to in your code.
I think it is applicable for C++11 too.

- 816
- 8
- 9

- 119
- 2
Good naming has a huge impact on understanding. With the zoo of very similarly named errorish things introduced in C++11...
std::errc - an error code
std::error_code - a different error code?
std::error_condition - yet another kind of error code
std::error_category - a utility helper interface for error codes
...I had to make a mental map just to sort them all out:
std::errc -> std::posix_error_code
std::error_code -> std::platform_dependent_error
std::error_condition -> std::platform_independent_error
std::error_category -> std::error_domain/utility/mapper
std::system_category -> std::platform_dependent_error_domain
std::generic_category -> std::platform_independent_error_domain
std::future_errc -> std::future_error_code
std::future_category -> std::future_error_domain
std::io_errc -> std::iostream_error_code
std::iostream_category -> std::iostream_error_domain
Both platform-dependent errors and platform-independent errors hold a code and a pointer to a utility helper class to map that domain-specific error codes to a message. Despite having nearly identical interfaces {assign, clear, value, message...}, they don't inherit from a common base class, meaning you can't generically call one or the other's methods. ♂️
Observe that std::error_code
is not an error code, but rather a combo of error code + plus domain/category, whereas std::errc
is an actual error code (a numeric value). Good ideas - awful naming .

- 2,034
- 1
- 24
- 39