我见过的提升实现了一个漂亮的帽子戏法,他们以某种方式使用()运算符重载的类boost ::系统:: ERROR_CODE的实例计算为一个布尔值
class error_code
{
...
typedef void (*unspecified_bool_type)();
static void unspecified_bool_true() {}
operator unspecified_bool_type() const // true if error
{
return m_val == 0 ? 0 : unspecified_bool_true;
}
...
}
Run Code Online (Sandbox Code Playgroud)
这导致可以检查这样的错误
...
boost::system::error_code err
some_boost_func(err);
if(err)
{
//handle error
}
....
Run Code Online (Sandbox Code Playgroud)
所以我一直在问自己..那里发生了什么?这似乎在某种程度上与函数指针的使用有关......如果我调用会发生什么,这会err评估函数本身还是函数指针?但是void (*unspecified_bool_type)();函数如何返回一个值
return m_val == 0 ? 0 : unspecified_bool_true;
Run Code Online (Sandbox Code Playgroud)