我可以让三元运算符像bool一样对待我的类吗? [英] Can I make the ternary operator treat my class like a bool?

查看:185
本文介绍了我可以让三元运算符像bool一样对待我的类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近做了一个巨大的重构,我改变了很多我的代码返回布尔值,而不是一个明确的返回代码。为了帮助这种重构,我决定尽可能地依靠编译器,让它告诉我我的代码需要改变的地方。我这样做是通过引入以下类(查看这里的低层如何工作)

  /// 
/// Typesafe布尔类别
///
class TypesafeBool
{
private:
bool m_bValue;
struct Bool_ {
int m_nValue;
};
typedef int Bool _ :: * bool_;
inline bool_ True()const {return& Bool _ :: m_nValue; }
inline bool_ False()const {return 0; }

public:
TypesafeBool(const bool bValue):m_bValue(bValue){}
operator bool_()const {return m_bValue?真假(); }
};

现在,不是使用普通的 bool 类型作为返回类型,我使用这个类,这意味着我不能再编译这样的东西:

  TypesafeBool SomeFunction (); 
long result = SomeFunction(); //错误

伟大的:通过让编译器做一个巨大的代码库很多的努力工作我。所以现在我已经完成了我的重构,我很想保持这个类,并继续使用它,因为它提供了额外的安全性,内置的 bool type不。



但是有一个问题阻止我这样做。目前,我们在我们的代码中大量使用三元运算符,问题是它不与这个没有显式转换的新类兼容:

  TypesafeBool result =(1 == 2?SomeFunction():false) ; //错误:使用不同的类型
TypesafeBool result =(1 == 2?SomeFunction():(TypesafeBool)false);

如果我能解决这个问题,以便我可以无缝地使用我的类可能在整个代码库中继续使用它。有人知道这个问题的解决方案吗?

解决方案

  class CCastableToBool 
{
public:
// ...
operator bool()const
{
// ...
{
return真正;
}
// ...
return false;
}
private:
// ...
};

但请注意,在C ++中,有一个类可以转换为bool被认为是非常危险的。您会收到警告: - )

您可以在这里阅读, SafeBool



您应该在所有的三元测试中明确调用TypesafeBool :: True()。

  TypesafeBool result =(1 == 2?SomeFunction()True():false); 


I've recently been doing a huge refactoring where I was changing a lot of my code to return booleans instead of an explicit return code. To aid this refactoring I decided to lean on the compiler where possible by getting it to tell me the places where my code needed to be changed. I did this by introducing the following class (see here for the lowdown on how this works):

///
/// Typesafe boolean class
///
class TypesafeBool
{
private:
    bool m_bValue;
    struct Bool_ { 
        int m_nValue; 
    };
    typedef int Bool_::* bool_;
    inline bool_ True() const { return &Bool_::m_nValue; }
    inline bool_ False() const { return 0; }

public:
    TypesafeBool( const bool bValue ) : m_bValue( bValue ){}
    operator bool_() const { return m_bValue ? True() : False(); }
};

Now, instead of using a normal bool type as the return type, I used this class which meant that I couldn't compile something like this any more:

TypesafeBool SomeFunction();
long result = SomeFunction(); // error

Great: it has made the refactoring manageable on a huge codebase by letting the compiler do a lot of the hard work for me. So now I've finished my refactoring and I'd quite like to keep this class hanging around and carry on using it since it affords us an extra level of safety that the built-in bool type doesn't.

There is however one "problem" which is preventing me from doing this. At the moment we make heavy use of the ternary operator in our code, and the problem is that it is not compatible with this new class without explicit casts:

TypesafeBool result = ( 1 == 2 ? SomeFunction() : false ); // error: different types used
TypesafeBool result = ( 1 == 2 ? SomeFunction() : (TypesafeBool)false );

If I could "solve" this issue so that I could use my class in a seamless manner I would probably carry on using it throughout the codebase. Does anyone know of a solution to this issue? Or is it just impossible to do what I want?

解决方案

      class CCastableToBool
      {  
      public:
         // ...
         operator bool() const
         {  
           //...
           {
              return true;
           } 
           //...
           return false;
         }  
      private:
         // ...
      }; 

but beware, in C++ it is considered really dangerous to have a class that can be casted to bool. You are warned :-)

you can read this there, SafeBool

You should explicitely call TypesafeBool::True() in all your ternary tests.

TypesafeBool result = ( 1 == 2 ? SomeFunction().True() : false );

这篇关于我可以让三元运算符像bool一样对待我的类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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