• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            不完美主義實(shí)踐者的哲學(xué)

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

             

            編譯期契約:約束

            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];
                  }
              };

             

            斷言

            建議使用斷言來(lái)斷言關(guān)于代碼結(jié)構(gòu)的條件式,而不要斷言關(guān)于運(yùn)行期行為的條件式。

            靜態(tài)/編譯期斷言:

            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; }

             

            域守衛(wèi)類

            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;
            };
            久久亚洲AV成人无码国产| 久久国产亚洲精品麻豆| 久久国产一片免费观看| 日韩电影久久久被窝网| 久久精品免费一区二区| 狠狠干狠狠久久| 久久男人中文字幕资源站| 亚洲日韩欧美一区久久久久我| 亚洲精品午夜国产VA久久成人| 伊人色综合九久久天天蜜桃 | 大伊人青草狠狠久久| 亚洲综合精品香蕉久久网97| 久久久久亚洲av成人无码电影| 97精品伊人久久久大香线蕉| 国产精品久久久久无码av| 性做久久久久久久久老女人| 91精品国产91久久久久福利| 国产欧美久久久精品影院| 欧美一区二区精品久久| 2021国产精品久久精品| 国产综合免费精品久久久| 18岁日韩内射颜射午夜久久成人| 国产成人精品综合久久久| 久久夜色精品国产噜噜噜亚洲AV| 久久久99精品成人片中文字幕| 久久精品国产亚洲AV香蕉| 超级97碰碰碰碰久久久久最新 | 久久久久噜噜噜亚洲熟女综合| 亚洲精品蜜桃久久久久久| 理论片午午伦夜理片久久| 久久香蕉综合色一综合色88| 久久香蕉国产线看观看精品yw| 香港aa三级久久三级老师2021国产三级精品三级在 | 色综合久久久久久久久五月| 狠狠色丁香久久婷婷综合蜜芽五月 | 伊人热热久久原色播放www| 91久久精品国产免费直播| 日韩AV无码久久一区二区| 亚洲欧美国产日韩综合久久| 国产精品欧美亚洲韩国日本久久| 国产婷婷成人久久Av免费高清|