这种向布尔的转换是如何工作的

How does this conversion to bool work?

本文关键字:何工作 工作 布尔 转换      更新时间:2023-10-16

我正在学习Cinder框架。这个框架中有一个类Texture,它可以这样使用:

Texture myImage;
myImage.loadImage(/*...*/);
if(myImage)
{
    // draw the image.
}

我对此感到困惑,因为myImage是一个对象。用它作为条件对我来说没有意义。我期待着myImage.exist();这样的东西。所以我遍历了代码,结果发现Texture类定义了一个转换运算符:

public:
    //@{
    //! Emulates shared_ptr-like behavior
    typedef std::shared_ptr<Obj> Texture::*unspecified_bool_type;
    // What is this???
    operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Texture::mObj; }
    void reset() { mObj.reset(); }
    //@}  

Obj定义为:

protected:      
    struct Obj {
        Obj() : mWidth( -1 ), mHeight( -1 ), mCleanWidth( -1 ), mCleanHeight( -1 ), mInternalFormat( -1 ), mTextureID( 0 ), mFlipped( false ), mDeallocatorFunc( 0 ) {}
        Obj( int aWidth, int aHeight ) : mInternalFormat( -1 ), mWidth( aWidth ), mHeight( aHeight ), mCleanWidth( aWidth ), mCleanHeight( aHeight ), mFlipped( false ), mTextureID( 0 ), mDeallocatorFunc( 0 )  {}
        ~Obj();
        mutable GLint   mWidth, mHeight, mCleanWidth, mCleanHeight;
        float           mMaxU, mMaxV;
        mutable GLint   mInternalFormat;
        GLenum          mTarget;
        GLuint          mTextureID;
        bool            mDoNotDispose;
        bool            mFlipped;   
        void            (*mDeallocatorFunc)(void *refcon);
        void            *mDeallocatorRefcon;            
    };
    std::shared_ptr<Obj>        mObj;

我知道operator int() const可以简单地将Object更改为int,但未指定的bool_type是如何工作的?当if(myImage)正在执行时,调试器在operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Texture::mObj; }处停止。

我可能对这里的语法有点困惑,是什么

typedef std::shared_ptr<Obj> Texture::*unspecified_bool_type;

刻薄?

void (*mDeallocatorFunc)(void *refcon); 

在Obj中,意思是mDeallocatorFunc是Class Obj的成员,Class Obj是指向原型为void xxx(void *)的函数的函数指针?

这是安全布尔习惯用法。它不使用简单的operator bool(),因为隐式转换可能会导致该运算符出现各种问题。因此,它使用了一个可以隐式转换为bool的类型(就像指向成员的指针),这是最不危险的。

幸运的是,C++11中不需要这种破解,因为我们可以编写explicit operator bool,而不会成为隐式转换的牺牲品。