Nov 22, 2017

[C++] error_code usage and design

Reference:
https://akrzemi1.wordpress.com/2017/07/12/your-own-error-code/
https://akrzemi1.wordpress.com/2017/08/12/your-own-error-condition/
https://akrzemi1.wordpress.com/2017/09/04/using-error-codes-effectively/

Header <system_error>

tl;dr;
std::error_code
  • provides value (int)  and catagory (std::error_category)
  • It's platform-dependent.
  • used for storing and transmitting error codes as they were produced by originating library, unchanged;


std::error_category
  • used to differentiate the error_code/error_condition's domain.
  • As a bridge, has member function equivalent to test out error_code and error_condition are in the same domain or not.
  • Has member function default_error_condition to provide error_condition base on error_code.


std::error_condition
  • provides value (int)  and category (std::error_category)
  • It's not platform-dependent.
  • used for performing queries on error_codes, for the purpose of grouping or classification or translation.

Design

  • One exception contains error_code present different meaning.
  • Make sure error_code.value has success value equals to 0.
  • enums in C++: we can create values from outside the enumerated range.
    It is for this reason that compilers issue a warning in switch-statement that
    “not all control paths return value” even though you have a case label for every enumeration.
  • std::is_error_code_enum<Errc>::value returns true, 
    If it's true, the enum type can be used to construct error_code.
  • Function make_error_code taking error_code is defined and accessible through
    argument-dependent lookup.
  • std::errc (enum) 
  • std::is_error_condition_enum
  • If it's true, the enum type can be used to construct error_condition.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.