unspecified_bool_type 手法

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

在 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<int> 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 的类往往本身就很小(例如智能指针),而且很可能原本是没有虚函数的,为了这么一个目的引入虚函数指针实在太不划算了。

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航