unspecified_bool_type 手法

来源:互联网 发布:morris.js api中文 编辑:程序博客网 时间:2024/04/29 01:38
在 C++ 中,有时候我们需要一个自定义类型能够支持 if(obj) 和 if(!obj) 之类的语法,也就是说

MyClass obj;
if(obj)
{
  //do something
}

if(!obj)
{
  //do something
}

这个需要在智能指针的实现中尤其明显,因为它可以保证与原生 C++ 指针在用法上的一致性。明显的解决方法是重载 operator bool() 转换,但是这样问题太多,Effective C++ 里面有讨论。还有一个办法是重载 operator ! ,但是这样我们就不得不用 if(!!obj) 这样丑陋的语法来表达 if(obj) 。

在 Boost 里面,广泛运用的是所谓的 unspecified_bool_type 手法,例如在 shared_ptr 里面:

    typedef T * this_type::*unspecified_bool_type;

    operator unspecified_bool_type() const // never throws
    {
        return px == 0? 0: &this_type::px;
    }

这样一来,

shared_ptr sp;
if(sp)
{}

if(!sp)
{}

会成功,而

int i = sp;

会失败。解决了前面所述的问题。

但是对于我们这样的懒人,每次少写几个字总是好的,而这个手法是如此常用,以至于我们想要把它提取出来,变成一个 safe_bool 基类:

class safe_bool
{
protected:
   void safe_bool_true() const {}
  
   typedef safe_bool    this_type; 
   typedef void (this_type::*safe_bool_type)() const;
};

现在,要用的时候只需要

class MyClass : public safe_bool
{
public:
   operator safe_bool_type() const // never throws
   { return is_false() ? 0 : &this_type::safe_bool_true; }

   //...
};

其中 is_false 是 MyClass 定义的,其具体的写法和语义可以由 MyClass 的作者决定。当然这种做法是太不彻底了,我们不希望把写 operator safe_bool_type 的任务留给继承者,所以,有了下面这个可行(但是很笨)的办法:

class safe_bool
{
private:
   void safe_bool_true() const {}
  
   typedef safe_bool    this_type; 
   typedef void (this_type::*safe_bool_type)() const;
  
protected:
   virtual bool operator!()const = 0;
  
public:
   operator safe_bool_type() const // never throws
   { return operator!() ? 0 : &this_type::safe_bool_true; }
};

它要求继承它的类重载 operator ! ,例如:

class MyClass : public safe_bool
{
   int data;
public:
   bool operator!() const
   { return data == 0; }
};

然而这绝对不是个优雅的办法,因为要求所有的继承者重载 operator ! 毕竟是有点无理,而且它引入了一个虚函数,由于继承 safe_bool 的类往往本身就很小(例如智能指针),而且很可能原本是没有虚函数的,为了这么一个目的引入虚函数指针实在太不划算了。

(还在想办法,可能有人已经做过了,欢迎交流)

---------------------------------------------------------------------------------------------------

看到了这篇文章,对这个主题讲得详细深入,和大家共享:

The Safe Bool Idiom
by Bjorn Karlsson

顺便也把它的程序贴过来备忘

  class safe_bool_base {
protected:
typedef void (safe_bool_base::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}

safe_bool_base() {}
safe_bool_base(const safe_bool_base&) {}
safe_bool_base& operator=(const safe_bool_base&) {return *this;}
~safe_bool_base() {}
};

template class safe_bool : public safe_bool_base {
public:
operator bool_type() const {
return (static_cast(this))->boolean_test()
? &safe_bool_base::this_type_does_not_support_comparisons : 0;
}
protected:
~safe_bool() {}
};

template<> class safe_bool : public safe_bool_base {
public:
operator bool_type() const {
return boolean_test()==true ?
&safe_bool_base::this_type_does_not_support_comparisons : 0;
}
protected:
virtual bool boolean_test() const=0;
virtual ~safe_bool() {}
};

template
void operator==(const safe_bool& lhs,const safe_bool& rhs) {
lhs.this_type_does_not_support_comparisons();
return false;
}

template
void operator!=(const safe_bool& lhs,const safe_bool& rhs) {
lhs.this_type_does_not_support_comparisons();
return false;
}

Here's how to use safe_bool:

  class Testable_with_virtual : public safe_bool<> {
protected:
bool boolean_test() const {
// Perform Boolean logic here
}
};

class Testable_without_virtual :
public safe_bool {
public:
bool boolean_test() const {
// Perform Boolean logic here
}
};




原创粉丝点击