fork(3) download
  1. #include <iostream>
  2. using namespace std;
  3. #include <system_error>
  4. #include <cassert>
  5.  
  6. namespace mylib
  7. {
  8. namespace errc {
  9.  
  10. enum my_error
  11. {
  12. failed = 0
  13. };
  14.  
  15. inline const char* error_message(int c)
  16. {
  17. static const char* err_msg[] =
  18. {
  19. "Failed",
  20. };
  21.  
  22. assert(c < sizeof(err_msg) / sizeof(err_msg[0]));
  23. return err_msg[c];
  24. }
  25.  
  26. class my_error_category : public std::error_category
  27. {
  28. public:
  29.  
  30. my_error_category()
  31. { }
  32.  
  33. std::string message(int c) const
  34. {
  35. return error_message(c);
  36. }
  37.  
  38. const char* name() const { return "My Error Category"; }
  39.  
  40. const static error_category& get()
  41. {
  42. const static my_error_category category_const;
  43. return category_const;
  44. }
  45. };
  46.  
  47. inline std::error_code make_error_code(my_error e)
  48. {
  49. return std::error_code(static_cast<int>(e), my_error_category::get());
  50. }
  51.  
  52. } // end namespace errc
  53. } // end namespace mylib
  54.  
  55. namespace std {
  56.  
  57. template<>
  58. struct is_error_code_enum<mylib::errc::my_error>
  59. : std::true_type
  60. { };
  61.  
  62. } // end namespace std
  63.  
  64. int main()
  65. {
  66. std::error_code ec1 = mylib::errc::make_error_code(mylib::errc::failed); // works
  67. std::error_code ec2 = mylib::errc::failed; // works
  68. bool result = (ec2 == mylib::errc::failed); // works
  69.  
  70. std::cout << ec1 << std::endl;
  71. }
Success #stdin #stdout 0s 2932KB
stdin
Standard input is empty
stdout
My Error Category:0