安全布尔成语bool_type(和安全布尔成语)如何工作? [英] How does the safe bool idiom bool_type (and the safe bool idiom) work?

查看:47
本文介绍了安全布尔成语bool_type(和安全布尔成语)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人指出安全布尔习语",并试图解释发生了什么(解释 不足以使我了解为什么起作用),我决定尝试将以下代码分开,并尝试简化为尽可能地.该网站提供了以下代码:

I was pointed to the 'safe bool idiom', and after trying to decipher what is going on (the explanation supplied on the site was not sufficient enough to grant me understanding of why it works), I decided to try to take the following code apart and make an attempt at simplifying it as much as possible. The site supplied code below:

class Testable {
    bool ok_;
    typedef void (Testable::*bool_type)() const;
    void this_type_does_not_support_comparisons() const {}
  public:
    explicit Testable(bool b=true):ok_(b) {}

    operator bool_type() const {
      return ok_==true ? 
        &Testable::this_type_does_not_support_comparisons : 0;
    }
  };

我决定分析'bool_type'的关键基础,因为这似乎是它的重点.给出以下行:

I decided to analyse the key basis of 'bool_type' given this seems to be what it's centred on. Given the following line:

typedef void (Testable::*bool_type)() const;

可以(由于括号而不容易)推断出它是一种类型为'void Testable :: *'的typedef,其中bool_type表示该类型定义.可以通过进行以下修改和函数调用来进一步证明这一点:

One can (not so easily, due to bracketing) deduce it's a typedef of a type of 'void Testable::*', of which bool_type represents. This can be further demonstrated by making the following modifications and function calls:

class Testable {
    bool ok_;
    typedef void (Testable::*bool_type)() const;
    void this_type_does_not_support_comparisons() const {}
  public:
    explicit Testable(bool b=true):ok_(b) {}

    bool_type Test; //Added this

    operator bool_type() const {
      return ok_==true ?
        &Testable::this_type_does_not_support_comparisons : 0;
    }
  };

int main()
{
    Testable Test;
    int A = Test.Test; //Compiler will give a conversion error, telling us what type .Test is in the process
}

它使我们可以看到bool_type是什么类型:

It allows us to see what type bool_type is:

错误:初始化时无法将'void(Testable :: *)()const'转换为'int'

error: cannot convert 'void (Testable::*)()const' to 'int' in initialization

这表明它确实是'void(Testable :: *)'的类型.

Which shows it is indeed a type of 'void (Testable::*)'.

问题出现在这里:

如果我们修改以下功能:

If we modify the following function:

    operator bool_type() const {
      return ok_==true ? 
        &Testable::this_type_does_not_support_comparisons : 0;
    }

并将其转换为:

    operator void Testable::* () const //Same as bool_type, right? 
    {
      return ok_==true ? 
        &Testable::this_type_does_not_support_comparisons : 0;
    }

它会产生以下投诉:

错误:"*"令牌之前的预期标识符
错误:<无效的运算符>'声明为返回函数的函数

error: expected identifier before '*' token
error: '< invalid operator >' declared as function returning a function

因此,我的问题是:

如果'void(Testable :: *)确实是bool_type的typedef,为什么会产生这些投诉?

Why is it generating those complaints if 'void (Testable::*) is indeed the typedef for bool_type?

还有

这是怎么回事?

推荐答案

您的推理在这里出错

operator void Testable::* () const //Same as bool_type, right? 

这是不正确的.正如编译器在错误消息中告诉我们的那样,bool_type的类型是:

This isn't correct. The type of bool_type is, as the compiler tells us in the error message:

'void(Testable :: *)()const'

'void (Testable::*)()const'

因此,要在运算符中替换它,您将需要

So, to replace it in the operator, you would need something like

operator (void (Testable::*)() const) () const

如果可能的话!看看为什么丑陋的typedef还是一个改进?

if that is ever possible! See why even the ugly typedef is an improvement?

在C ++ 11中,我们还有新的构造体 explicit运算符bool(),以免我们受此丑陋之害.

In C++11 we also have the new construct explicit operator bool() to save us from this ugliness.

这篇关于安全布尔成语bool_type(和安全布尔成语)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆