<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    so true

    心懷未來,開創未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數據加載中……

    safe bool conversion

    現在越來越懶了,雖然積累的知識越來越多,但寫出來的東西越來越少。
    趁還有點心情,把剛看的關于safe bool的東西寫到這里:

    相關url: http://www.artima.com/cppsource/safeboolP.html

    這頁面上的code有問題,經過調試,并借鑒了一部分http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool里面提到的東西,整理了一份可以編譯的code:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>

    using namespace std;

      class safe_bool_base {
      protected:
        void this_type_does_not_support_comparisons() const {}
        typedef void (safe_bool_base::*bool_type)() const;

        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 : private safe_bool_base {
      public:
        operator bool_type() const {
          return (static_cast<const T*>(this))->boolean_test() ? &safe_bool<T>::this_type_does_not_support_comparisons : 0;
        }
      protected:
        ~safe_bool() {}
      };

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

      template <typename T>
       bool operator==(const safe_bool<T>& lhs, bool b) {
          return (lhs) ? b : !b;
      }

      template <typename T>
       bool operator==(bool b, const safe_bool<T>& rhs) {
          return (rhs) ? b : !b;
      }

      template <typename T, typename U>
        void operator==(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
          lhs.this_type_does_not_support_comparisons();
          return false;
      }

      template <typename T,typename U>
      void operator!=(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
        lhs.this_type_does_not_support_comparisons();
        return false;  
      }

      class Testable_with_virtual : public safe_bool<> {
      protected:
        bool boolean_test() const {
          // Perform Boolean logic here
          return true;
        }
      };

    #if (1)
      class Testable_without_virtual :
        public safe_bool <Testable_without_virtual> {
      public:
        bool boolean_test() const {
          // Perform Boolean logic here
          return true;
        }
      };
    #else
      class Testable_without_virtual :
        private safe_bool <Testable_without_virtual> {
        template <typename T> friend class safe_bool;
      public:
        using safe_bool<Testable_without_virtual>::operator bool_type;

        bool boolean_test() const {
          return true; // Logic goes here!
        }
      };
    #endif

    int main(int argc, char* argv[])
    {
        Testable_without_virtual t;
        if (t) {
            cout << "OK" << endl;
        } else {
            cout << "FAIL" << endl;
        }

        Testable_with_virtual t2;
        if (t2 == true && true == t2) {
            cout << "OK" << endl;
        } else {
            cout << "FAIL" << endl;
        }

        return 0;
    }

    當然,上面的代碼里有很多技巧,不光是safe bool idiom,對于平日里寫代碼時,值得借鑒的是:
    class A {
        typedef void* A::*unspecified_bool_type;
    public:
        /* //別再寫這這樣的conversion了,用下面的那個
        operator bool() const {
            return _pointer ? true : false;
        }
        */
        operator unspecified_bool_type() const {
            return _pointer ? &A::_pointer : NULL;
        }

    private:
        void* _pointer;
    };


    posted on 2011-01-25 16:08 so true 閱讀(313) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 91精品免费在线观看| 国产无遮挡又黄又爽免费视频 | 亚洲精品国产精品乱码不卞| 亚洲欧美乱色情图片| 黄色a三级免费看| 日日操夜夜操免费视频| 日韩国产欧美亚洲v片| 青柠影视在线观看免费高清 | 免费人成网站在线播放| 337p日本欧洲亚洲大胆人人| 本道天堂成在人线av无码免费| 亚洲成av人片不卡无码久久| 婷婷亚洲综合五月天小说| 亚洲国产美女精品久久久| 在线观看视频免费国语| 亚洲永久精品ww47| 免费无码又爽又刺激网站直播| 在线观看国产区亚洲一区成人 | 一级特黄录像视频免费| 巨波霸乳在线永久免费视频| 亚洲女人被黑人巨大进入| 日韩毛片在线免费观看| 久久久久国产成人精品亚洲午夜 | 亚洲国产成人久久精品软件| 免费无码不卡视频在线观看| 免费观看四虎精品成人| 亚洲熟妇av一区二区三区漫画| 久久青草免费91观看| 亚洲人成网站在线观看播放动漫| 大学生高清一级毛片免费| 污网站免费在线观看| 精品亚洲一区二区| 在线a级毛片免费视频| 特级毛片aaaa级毛片免费| 亚洲女初尝黑人巨高清| 黄页网站在线观看免费高清| 羞羞视频免费网站入口| 国产亚洲精品a在线观看app| 国产精品一区二区三区免费 | 99热这里有免费国产精品| 亚洲欧美国产欧美色欲|