mercredi 26 août 2015

Use cases for std::error_code

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 :

  1. Be dealing with a system specific error reporting mechanism (like say, errno or something returned from a POSIX call, or say, a call to getsockopt with SO_ERROR on Linux) which you can easily convert to an std::error_condition via the std::errc enums, which are supposed to be portable.

  2. 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.

  3. 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 like std::error_code, why not just convert to std::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?

Aucun commentaire:

Enregistrer un commentaire