#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;
}
} ;
} // end namespace errc
} // end namespace mylib
namespace std {
inline error_code make_error_code( mylib:: errc :: my_error e)
{
return error_code( static_cast < int > ( e) , mylib:: errc :: my_error_category :: get ( ) ) ;
}
template <>
struct is_error_code_enum< mylib:: errc :: my_error >
: std:: true_type
{ } ;
} // end namespace std
int main( )
{
std:: error_code ec1 = std:: make_error_code ( mylib:: errc :: failed ) ; // works
std:: error_code ec2 = mylib:: errc :: failed ; // doesn't compile
bool result = ( ec2 == mylib:: errc :: failed ) ; // doesn't compile
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojaW5jbHVkZSA8c3lzdGVtX2Vycm9yPgojaW5jbHVkZSA8Y2Fzc2VydD4KCm5hbWVzcGFjZSBteWxpYgp7CiAgICBuYW1lc3BhY2UgZXJyYyB7CgogICAgICAgIGVudW0gbXlfZXJyb3IKICAgICAgICB7CiAgICAgICAgICAgIGZhaWxlZCA9IDAKICAgICAgICB9OwoKICAgICAgICBpbmxpbmUgY29uc3QgY2hhciogZXJyb3JfbWVzc2FnZShpbnQgYykKICAgICAgICB7CiAgICAgICAgICAgIHN0YXRpYyBjb25zdCBjaGFyKiBlcnJfbXNnW10gPSAKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgIkZhaWxlZCIsCiAgICAgICAgICAgIH07CgogICAgICAgICAgICBhc3NlcnQoYyA8IHNpemVvZihlcnJfbXNnKSAvIHNpemVvZihlcnJfbXN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ZXJyb3I+CiAgICA6IHN0ZDo6dHJ1ZV90eXBlCnsgfTsgCgp9IC8vIGVuZCBuYW1lc3BhY2Ugc3RkCgppbnQgbWFpbigpCnsKICAgIHN0ZDo6ZXJyb3JfY29kZSBlYzEgPSBzdGQ6Om1ha2VfZXJyb3JfY29kZShteWxpYjo6ZXJyYzo6ZmFpbGVkKTsgLy8gd29ya3MKICAgIHN0ZDo6ZXJyb3JfY29kZSBlYzIgPSBteWxpYjo6ZXJyYzo6ZmFpbGVkOyAvLyBkb2Vzbid0IGNvbXBpbGUKICAgIGJvb2wgcmVzdWx0ID0gKGVjMiA9PSBteWxpYjo6ZXJyYzo6ZmFpbGVkKTsgLy8gZG9lc24ndCBjb21waWxlCn0K
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