fork(1) 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. } // end namespace errc
  48. } // end namespace mylib
  49.  
  50. namespace std {
  51.  
  52. inline error_code make_error_code(mylib::errc::my_error e)
  53. {
  54. return error_code(static_cast<int>(e), mylib::errc::my_error_category::get());
  55. }
  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 = std::make_error_code(mylib::errc::failed); // works
  67. std::error_code ec2 = mylib::errc::failed; // doesn't compile
  68. bool result = (ec2 == mylib::errc::failed); // doesn't compile
  69. }
  70.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from prog.cpp:3:0:
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/system_error: In constructor 'std::error_code::error_code(_ErrorCodeEnum, typename std::enable_if<std::is_error_code_enum<_ErrorCodeEnum>::value>::type*) [with _ErrorCodeEnum = mylib::errc::my_error, typename std::enable_if<std::is_error_code_enum<_ErrorCodeEnum>::value>::type = void]':
prog.cpp:67:40:   instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/system_error:127:9: error: cannot convert 'mylib::errc::my_error' to 'std::errc' for argument '1' to 'std::error_code std::make_error_code(std::errc)'
stdout
Standard output is empty