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

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

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

    so true

    心懷未來(lái),開(kāi)創(chuàng)未來(lái)!
    隨筆 - 160, 文章 - 0, 評(píng)論 - 40, 引用 - 0
    數(shù)據(jù)加載中……

    safe bool conversion

    現(xiàn)在越來(lái)越懶了,雖然積累的知識(shí)越來(lái)越多,但寫出來(lái)的東西越來(lái)越少。
    趁還有點(diǎn)心情,把剛看的關(guān)于safe bool的東西寫到這里:

    相關(guān)url: http://www.artima.com/cppsource/safeboolP.html

    這頁(yè)面上的code有問(wèn)題,經(jīng)過(guò)調(diào)試,并借鑒了一部分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;
    }

    當(dāng)然,上面的代碼里有很多技巧,不光是safe bool idiom,對(duì)于平日里寫代碼時(shí),值得借鑒的是:
    class A {
        typedef void* A::*unspecified_bool_type;
    public:
        /* //別再寫這這樣的conversion了,用下面的那個(gè)
        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 閱讀(314) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产91免费在线观看| 可以免费观看的一级毛片| 免费影院未满十八勿进网站| 精品国产免费观看一区| 精品亚洲成α人无码成α在线观看 | 深夜a级毛片免费无码| 99在线热视频只有精品免费| 成人永久免费福利视频网站| 亚洲VA成无码人在线观看天堂 | 亚洲国产三级在线观看| 丁香婷婷亚洲六月综合色| 国产精品高清免费网站| www.黄色免费网站| 国产成人亚洲精品狼色在线| 亚洲精品蜜夜内射| 美女内射毛片在线看免费人动物| 国产亚洲精品无码专区| 国产在线观看无码免费视频| 四虎永久免费地址在线网站| 亚洲一区二区三区高清视频| 国产VA免费精品高清在线| 中文字幕亚洲乱码熟女一区二区| 久久久久免费视频| 亚洲高清免费视频| 亚洲av成人一区二区三区观看在线| 99爱在线精品视频免费观看9| 亚洲欧洲高清有无| 黄网站色视频免费在线观看的a站最新| 国产精品色午夜免费视频| 亚洲 欧洲 视频 伦小说| 国产四虎免费精品视频| 亚洲欧好州第一的日产suv| 国产一卡2卡3卡4卡无卡免费视频 国产一卡二卡3卡四卡免费 | 亚洲暴爽av人人爽日日碰| 亚洲一级特黄大片无码毛片| 国产精品亚洲二区在线| 国产a不卡片精品免费观看| 中文日本免费高清| 亚洲愉拍一区二区三区| 午夜毛片不卡高清免费| 亚洲中文精品久久久久久不卡|