• <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 閱讀(877) 評論(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;
            };
            婷婷久久综合九色综合绿巨人 | 狠狠久久综合伊人不卡| 9999国产精品欧美久久久久久| 久久91精品久久91综合| 久久激情亚洲精品无码?V| 久久夜色精品国产噜噜亚洲a| 无码专区久久综合久中文字幕| 久久99国产精品久久99果冻传媒| 国产精品99久久精品爆乳| 久久精品国产亚洲AV香蕉| 久久久久久综合一区中文字幕 | 久久AV高清无码| 香蕉久久影院| 香蕉久久夜色精品国产小说| 波多野结衣久久| 日本精品久久久久中文字幕| 亚洲伊人久久精品影院| 久久精品中文字幕有码| 国产午夜久久影院| 亚洲午夜无码久久久久| 热综合一本伊人久久精品| 亚洲午夜精品久久久久久人妖| 思思久久好好热精品国产| 国产精品亚洲综合专区片高清久久久| 亚洲精品视频久久久| 久久播电影网| 91精品婷婷国产综合久久| 久久久久免费看成人影片| 一级做a爰片久久毛片毛片| 久久久久成人精品无码| 99久久国产综合精品五月天喷水| 久久久久国产精品熟女影院 | 久久不见久久见免费影院www日本| 久久人人爽人人爽人人AV| 久久久久国产精品人妻| 国产高潮国产高潮久久久91| 久久久久一区二区三区| 久久91亚洲人成电影网站| 狠狠狠色丁香婷婷综合久久五月 | 一本久久a久久精品综合夜夜| 国内精品久久久久|