#include <iostream>
using namespace std;
#include <system_error>
#include <cassert>
namespace mylib
{
namespace errc {
enum my_error
{
failed = 0
};
inline const char* error_message(int c)
{
static const char* err_msg[] =
{
"Failed",
};
assert(c < sizeof(err_msg) / sizeof(err_msg[0]));
return err_msg[c];
}
class my_error_category : public std::error_category
{
public:
my_error_category()
{ }
std::string message(int c) const
{
return error_message(c);
}
const char* name() const { return "My Error Category"; }
const static error_category& get()
{
const static my_error_category category_const;
return category_const;
}
};
inline std::error_code make_error_code(my_error e)
{
return std::error_code(static_cast<int>(e), my_error_category::get());
}
} // end namespace errc
} // end namespace mylib
namespace std {
template<>
struct is_error_code_enum<mylib::errc::my_error>
: std::true_type
{ };
} // end namespace std
int main()
{
std::error_code ec1 = mylib::errc::make_error_code(mylib::errc::failed); // works
std::error_code ec2 = mylib::errc::failed; // works
bool result = (ec2 == mylib::errc::failed); // works
std::cout << ec1 << std::endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojaW5jbHVkZSA8c3lzdGVtX2Vycm9yPgojaW5jbHVkZSA8Y2Fzc2VydD4KCm5hbWVzcGFjZSBteWxpYgp7CiAgICBuYW1lc3BhY2UgZXJyYyB7CgogICAgICAgIGVudW0gbXlfZXJyb3IKICAgICAgICB7CiAgICAgICAgICAgIGZhaWxlZCA9IDAKICAgICAgICB9OwoKICAgICAgICBpbmxpbmUgY29uc3QgY2hhciogZXJyb3JfbWVzc2FnZShpbnQgYykKICAgICAgICB7CiAgICAgICAgICAgIHN0YXRpYyBjb25zdCBjaGFyKiBlcnJfbXNnW10gPSAKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgIkZhaWxlZCIsCiAgICAgICAgICAgIH07CgogICAgICAgICAgICBhc3NlcnQoYyA8IHNpemVvZihlcnJfbXNnKSAvIHNpemVvZihlcnJfbXNnWzBdKSk7CiAgICAgICAgICAgIHJldHVybiBlcnJfbXNnW2NdOwogICAgICAgIH0KCiAgICAgICAgY2xh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