[英]Comparing an error code enum with std::error_code
I'm using the C++11 system_error
error code library to create a custom error class for a library I'm making. 我正在使用C ++ 11
system_error
错误代码库为我正在创建的库创建自定义错误类。 I've done this before with boost::error_code
, but I can't quite it get it working with std::error_code
. 我之前使用
boost::error_code
完成了这个,但我不能完全使用std::error_code
。 I'm using GCC 4.6. 我正在使用GCC 4.6。
Basically, I've laid out all the boilerplate code to create an error class, an error_category, and the conversion routines in the STD namespace to convert my custom enums into an std::error_code
object: 基本上,我已经列出了所有的样板代码来创建一个错误类,一个error_category,以及STD命名空间中的转换例程,以将我的自定义枚举转换为
std::error_code
对象:
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
{ };
The problem is, implicit conversion between my error code enums and std::error_code
objects doesn't seem to be working, so I can't for example try and compare an instance of std::error_code
with enum literals: 问题是,我的错误代码枚举和
std::error_code
对象之间的隐式转换似乎不起作用,所以我不能尝试将std::error_code
的实例与枚举文字进行比较:
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
}
The expression ec2 == mylib::errc::failed
won't compile - I have to say ec2 == std::make_error_code(mylib::errc::failed)
. 表达式
ec2 == mylib::errc::failed
将无法编译 - 我不得不说ec2 == std::make_error_code(mylib::errc::failed)
。
The error the compiler emits is: 编译器发出的错误是:
In file included from test6.cc:3:0:
/usr/include/c++/4.6/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]’:
test6.cc:70:37: instantiated from here
/usr/include/c++/4.6/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)’
And here's an Ideone link . 这是一个Ideone链接 。
So, why isn't this working? 那么,为什么这不起作用? Do I need additional boilerplate code to enable
mylib::errc::my_error
enums to be implicitly convertible to std::error_code
? 我是否需要额外的样板代码才能使
mylib::errc::my_error
枚举可以隐式转换为std::error_code
? I thought that the specialization of std::make_error_code
takes care of that? 我认为
std::make_error_code
的专业化可以解决这个问题吗?
You have to move error_code make_error_code(mylib::errc::my_error e)
function from std
to your error namespace ( mylib::errc
). 您必须将
error_code make_error_code(mylib::errc::my_error e)
函数从std
到您的错误命名空间( mylib::errc
)。 Please check http://ideone.com/eSfee . 请查看http://ideone.com/eSfee 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.