C++;安全布尔习语无法使用Visual C++;10(2010) 嘿,伙计们,我从这个页面中的C++安全语言习惯类中派生出了我的类:

C++;安全布尔习语无法使用Visual C++;10(2010) 嘿,伙计们,我从这个页面中的C++安全语言习惯类中派生出了我的类:,c++,safe-bool-idiom,C++,Safe Bool Idiom,我得到一个错误C2451:“Element”类型的条件表达式是非法的。如果我试着像下面这样把它投射到布尔,我会得到这个错误 Element ele; ... if((bool)ele) 错误C2440:“类型转换”:无法从“元素”转换为“布尔” 这是我第一次使用安全BooL习语,我不确定这是不允许的还是VisualC++ 10中的bug。有什么评论吗?提前谢谢 安全bool习惯用法是允许的,尽管我通常这样写: class Element { public: bool Exists()

我得到一个错误C2451:“Element”类型的条件表达式是非法的。如果我试着像下面这样把它投射到布尔,我会得到这个错误

Element ele;
...
if((bool)ele)
错误C2440:“类型转换”:无法从“元素”转换为“布尔”


这是我第一次使用安全BooL习语,我不确定这是不允许的还是VisualC++ 10中的bug。有什么评论吗?提前谢谢

安全bool习惯用法是允许的,尽管我通常这样写:

class Element
{
public:
    bool Exists() const;

    /* Begin Safe Bool Idiom */

private:
    // This is a typedef for pointer to an int member of Element.
    typedef int Element::*SafeBoolType;
public:
    inline operator SafeBoolType() const
        { return Exists() ? &Element::someDataMember : 0; }
    inline bool operator!() const
        { return !Exists(); }

    /* End Safe Bool Idiom */

private:
    int someDataMember; // Pick any data member
    // ...
};

这就是我看到它实现的方式。事实上,Boost以这种方式为智能指针类()实现了这个习惯用法。

安全bool习惯用法是允许的,尽管我通常这样写:

class Element
{
public:
    bool Exists() const;

    /* Begin Safe Bool Idiom */

private:
    // This is a typedef for pointer to an int member of Element.
    typedef int Element::*SafeBoolType;
public:
    inline operator SafeBoolType() const
        { return Exists() ? &Element::someDataMember : 0; }
    inline bool operator!() const
        { return !Exists(); }

    /* End Safe Bool Idiom */

private:
    int someDataMember; // Pick any data member
    // ...
};

这就是我看到它实现的方式。事实上,Boost以这种方式为智能指针类()实现了这个习惯用法。

它似乎没有使用任何编译器编译。显然,
safe\u bool
无法返回受保护方法的基地址。您应该将公共方法添加到
safe\u bool\u base
并返回该方法的地址

此外,运算符
==
似乎也是如此=(即使未实例化,也可能导致错误)

也许这可以解决问题:

 class safe_bool_base {
  protected:
    typedef void (safe_bool_base::*bool_type)() const;
  private:
    void cannot_compare_boolean_results() const {}
  public:
    void public_func() const {}
  protected:
    safe_bool_base() {}
    safe_bool_base(const safe_bool_base&) {}
    safe_bool_base& operator=(const safe_bool_base&) {return *this;}
    ~safe_bool_base() {}
  };

  template <typename T=void> class safe_bool : public safe_bool_base {
  public:
    operator bool_type() const {
      return (static_cast<const T*>(this))->boolean_test()
        ? &safe_bool_base::public_func : 0;
    }
  protected:
    ~safe_bool() {}
  };

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

  template <typename T, typename U> 
    bool operator==(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
      lhs.cannot_compare_boolean_results(); //call private method to produce error
      return false;
  }

  template <typename T,typename U> 
  bool operator!=(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
    lhs.cannot_compare_boolean_results(); //call private method to produce error
    return false;   
  }
class-safe\u-bool\u-base{
受保护的:
typedef void(safe_bool_base::*bool_type)()const;
私人:
void无法比较布尔值结果()常量{}
公众:
void public_func()常量{}
受保护的:
安全布尔基(){}
安全布尔布尔布尔基(const safe布尔布尔基&){
safe_bool_base&运算符=(const safe_bool_base&){return*this;}
~safe_bool_base(){}
};
模板类安全库:公共安全库{
公众:
运算符bool_type()常量{
return(static_cast(this))->boolean_test()
?&safe\u bool\u base::public\u func:0;
}
受保护的:
~safe_bool(){}
};
模板类安全库:公共安全库{
公众:
运算符bool_type()常量{
返回布尔值_test()==真?
&安全布尔基::公共函数:0;
}
受保护的:
虚拟布尔测试()常量=0;
虚拟~safe_bool(){}
};
模板
布尔运算符==(常数安全布尔&左、常数安全布尔&右){
lhs.cannot_compare_boolean_results();//调用private方法以产生错误
返回false;
}
模板
接线员=(施工安全区和左侧、施工安全区和右侧){
lhs.cannot_compare_boolean_results();//调用private方法以产生错误
返回false;
}

它似乎不是用任何编译器编译的。显然,
safe\u bool
无法返回受保护方法的基地址。您应该将公共方法添加到
safe\u bool\u base
并返回该方法的地址

此外,运算符
==
似乎也是如此=(即使未实例化,也可能导致错误)

也许这可以解决问题:

 class safe_bool_base {
  protected:
    typedef void (safe_bool_base::*bool_type)() const;
  private:
    void cannot_compare_boolean_results() const {}
  public:
    void public_func() const {}
  protected:
    safe_bool_base() {}
    safe_bool_base(const safe_bool_base&) {}
    safe_bool_base& operator=(const safe_bool_base&) {return *this;}
    ~safe_bool_base() {}
  };

  template <typename T=void> class safe_bool : public safe_bool_base {
  public:
    operator bool_type() const {
      return (static_cast<const T*>(this))->boolean_test()
        ? &safe_bool_base::public_func : 0;
    }
  protected:
    ~safe_bool() {}
  };

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

  template <typename T, typename U> 
    bool operator==(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
      lhs.cannot_compare_boolean_results(); //call private method to produce error
      return false;
  }

  template <typename T,typename U> 
  bool operator!=(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
    lhs.cannot_compare_boolean_results(); //call private method to produce error
    return false;   
  }
class-safe\u-bool\u-base{
受保护的:
typedef void(safe_bool_base::*bool_type)()const;
私人:
void无法比较布尔值结果()常量{}
公众:
void public_func()常量{}
受保护的:
安全布尔基(){}
安全布尔布尔布尔基(const safe布尔布尔基&){
safe_bool_base&运算符=(const safe_bool_base&){return*this;}
~safe_bool_base(){}
};
模板类安全库:公共安全库{
公众:
运算符bool_type()常量{
return(static_cast(this))->boolean_test()
?&safe\u bool\u base::public\u func:0;
}
受保护的:
~safe_bool(){}
};
模板类安全库:公共安全库{
公众:
运算符bool_type()常量{
返回布尔值_test()==真?
&安全布尔基::公共函数:0;
}
受保护的:
虚拟布尔测试()常量=0;
虚拟~safe_bool(){}
};
模板
布尔运算符==(常数安全布尔&左、常数安全布尔&右){
lhs.cannot_compare_boolean_results();//调用private方法以产生错误
返回false;
}
模板
接线员=(施工安全区和左侧、施工安全区和右侧){
lhs.cannot_compare_boolean_results();//调用private方法以产生错误
返回false;
}

您是否尝试过使用
类元素:公共安全布尔
?我知道这不完全是同一回事,但它是否与CRTP一起工作?“类元素:公共安全”不起作用。什么是CRTP?您是否尝试过使用
类元素:公共安全\u bool
?我知道这不完全是同一回事,但它是否与CRTP一起工作?“类元素:公共安全”不起作用。什么是CRTP?我可以编译本文中的代码,只是当我在条件表达式中使用我的类时,它无法编译。即使您修改的代码生成“错误C2451:Element类型的条件表达式是非法的”,我也可以编译本文中的代码,只是当我在条件表达式中使用我的类时,它无法编译。即使修改后的代码也会生成“错误C2451:类型为'Element'的条件表达式是非法的”,我想对我来说使用安全bool习惯用法是个坏主意,因为在我的类中,已经有了运算符int()方法。元素是一个数据变量类。我放弃了“保险箱”这个成语的想法。谢谢你的示例代码@尚旺:你试过看它是否有效吗?typedef不必指向
int
——您可以让它指向任何数据成员。另外,首先使用像
operator int()
这样的转换运算符通常不是最好的主意。我想使用safe-bool习惯用法对我来说是个坏主意,因为在我的类中,已经有了一个operator int()方法。元素是一个数据变量类。我放弃了“保险箱”这个成语的想法。谢谢你的示例代码@尚旺:你试过看它是否有效吗?typedef不必指向<