• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 13, comments - 4, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            Imperfect C++ 讀書筆記(一)

            Posted on 2008-11-17 22:27 Batiliu 閱讀(878) 評論(0)  編輯 收藏 引用 所屬分類: 讀書筆記

            不完美主義實踐者的哲學

            • 原則1————C++是卓越的,但并不完美。
            • 原則2————穿上“苦行衣”。
            • 原則3————讓編譯器成為你的仆從。
            • 原則4————永不言棄,總會有解決方案的。

             

            編譯期契約:約束

            1. must_have_base

              template<typename D, typename B>
              struct must_have_base
              {
                  ~must_have_base()
                  {
                      void (*p)(D*, B*) = constraints;
                  }
              private:
                  static void constraints(D* pd, B* pb)
                  {
                      pb = pd;
                  }
              };

            2. must_be_subscriptable

              template<typename T>
              struct must_be_subscriptable
              {
                  ~must_be_subscriptable()
                  {
                      void (*p)(T const &) = constraints;
                  }
              private:
                  static void constraints(T const &T_is_not_subscriptable)
                  {
                      sizeof(T_is_not_subscriptable[0]);
                  }
              };

            3. must_be_subscriptable_as_decayable_pointer

              template<typename T>
              struct must_be_subscriptable_as_decayable_pointer
              {
                  ~must_be_subscriptable_as_decayable_pointer()
                  {
                      void (*p)(T const &) = constraints;
                  }
              private:
                  static void constraints(T const &T_is_not_decay_subscriptable)
                  {
                      sizeof(0[T_is_not_decay_subscriptable]);    // offset[pointer]
                  }
              };

            4. must_be_pod

              template<typename T>
              struct must_be_pod
              {
                  ~must_be_pod()
                  {
                      void (*p)() = constraints;
                  }
              private:
                  static void constraints()
                  {
                      union
                      {
                          T T_is_not_POD_type;
                      };
                  }
              };

            5. must_be_same_size

              template<typename T1, typename T2>
              struct must_be_same_size
              {
                  ~must_be_same_size()
                  {
                      void (*p)() = constraints;
                  }
              private:
                  static void constraints()
                  {
                      const int T1_not_same_size_as_T2 = sizeof(T1) == sizeof(T2);
                      int i[T1_not_same_size_as_T2];
                  }
              };

             

            斷言

            建議使用斷言來斷言關于代碼結構的條件式,而不要斷言關于運行期行為的條件式。

            靜態/編譯期斷言:

            1. #define STATIC_ASSERT(ex) \
                  do { typedef int ai[(ex) ? 1 : 0]; } while(0)
            2. #define STATIC_ASSERT(ex) \
                  switch(0) { case 0: case ex: ; }
            3. #define STATIC_ASSERT(ex) \
                  struct X { unsigned int v : ex; }

             

            域守衛類

            template<typename L>
            struct lock_traits
            {
                static void lock(L &c)
                {
                    lock_instance(c);
                }
                static void unlock(L &c)
                {
                    unlock_instance(c);
                }
            };
             
            template<typename L, typename T = lock_traits<L> >
            class lock_scope
            {
            public:
                lock_scope(L &l)
                    : m_l(l)
                {
                    T::lock(m_l);
                }
                ~lock_scope()
                {
                    T::unlock(m_l);
                }
            private:
                L &m_l;
            };
            久久99精品久久久久久9蜜桃| 精品多毛少妇人妻AV免费久久| 久久er国产精品免费观看2| 成人久久久观看免费毛片| 久久精品国产亚洲Aⅴ蜜臀色欲| 香蕉aa三级久久毛片| 久久婷婷国产综合精品| 国产精品成人久久久久三级午夜电影 | 伊人久久精品影院| 99久久精品国内| 亚洲国产精品综合久久一线| 久久久免费精品re6| 久久婷婷五月综合97色直播| 国产三级久久久精品麻豆三级| 久久精品国产亚洲Aⅴ香蕉| 国产成人久久AV免费| 亚洲国产小视频精品久久久三级| 精品无码久久久久国产| 一级做a爰片久久毛片毛片| 色综合久久最新中文字幕| 97久久婷婷五月综合色d啪蜜芽 | 国产99久久九九精品无码| 亚洲精品无码久久久久去q| 亚洲精品tv久久久久| 久久久久久极精品久久久| 国产激情久久久久影院老熟女| 久久精品人人槡人妻人人玩AV| 亚洲国产另类久久久精品 | 色狠狠久久AV五月综合| 欧美国产成人久久精品| 久久久久久一区国产精品| 色综合色天天久久婷婷基地| 国产综合久久久久久鬼色| 国产午夜精品久久久久免费视| 精品国产青草久久久久福利| 久久亚洲精品无码aⅴ大香| 久久精品国产亚洲AV影院| 狠狠色狠狠色综合久久| 久久这里只有精品18| 91精品国产乱码久久久久久| 精品久久久久久久久中文字幕|