traits技術(shù)

  • 例子1

      template <typename T>
      struct my_is_void{
        static const boo value = false;
      };
      
      template <>
      strcut my_is_void<void>{
        static const bool value = true;
      };
      
      cout << my_is_void<int>::value << endl;
      cout << my_is_void<void>::value << endl;
    

    上面這個(gè)例子就是利用模板偏特化以及類靜態(tài)變量實(shí)現(xiàn)對類型判斷是否謂空類型的例子。這個(gè)例子只是引自,下面講2個(gè)實(shí)用的例子。

  • 例子2

      template <typename Iterator>
      void func(Iterator iter){
          //  我們想用迭代器的類型定義一個(gè)變量該怎么辦?
          *iter var // 這樣是不能通過編譯的。
      }
      
      // 轉(zhuǎn)換思路
      template <typename Iterator>
      void func(Iterator iter){
           __func(&*iter);
      }
    
      template <typename T>
      void __func(T* ){
          T var;
      }
    
  • type_traits
    在C++中有的比如int, bool, char等類型沒有必要有構(gòu)造,拷貝構(gòu)造等函數(shù),我們稱之為trivial_default_constructor,trivial_copy_constructor,trivial_assignment_operator,trivial_destructor,我們稱之為POD_type。反之稱之為no_POD_type。這個(gè)定義可能不嚴(yán)謹(jǐn),但不是今天這篇文的重點(diǎn)。

    在SGI STL底層的有這樣一個(gè)函數(shù)。目的是析構(gòu)[ first, last )的元素

       template <typename ForwardIterator>
       inline void destory(ForwardIterator first, ForwardIterator last) {
              if (is_POD_type(*first))
                  //....是int等類型,不需要析構(gòu)。
              if (is_no_POD_type(*first))
                  for (; first != last; first++) 
                        first->~(*first)()    // 調(diào)用析構(gòu)函數(shù),(*first)不能通過編譯。
       }  
    

    但是怎么表現(xiàn)is_POD_type和is_no_POD_type這2個(gè)意思了?這個(gè)時(shí)候我們可以用內(nèi)嵌型別和偏特化來實(shí)現(xiàn)。

      // 標(biāo)明類型
      struct _true_type  {  };
      struct _false_type  {  };
      
      // 用于萃取是否為POD_type
      template <typename T>
      struct _type_traits{  
          typedef _false_type has_trivial_default_constructor;
          typedef _false_type has_trivial_copy_constructor;
          typedef _false_type has_trivial_assignment_operator;
          typedef _false_type has_trivial_destructor;
          typedef _false_type is_POD_type;
      };
      
      // char偏特化
      template<>
      struct _type_traits<char> {
          typedef _true_type has_trivial_default_constructor;
          typedef _true_type has_trivial_copy_constructor;
          typedef _true_type has_trivial_assignment_operator;
          typedef _true_type has_trivial_destructor;
          typedef _true_type is_POD_type;
      };
      
      // int偏特化
      template<>
      struct _type_traits<int> {
          typedef _true_type has_trivial_default_constructor;
          typedef _true_type has_trivial_copy_constructor;
          typedef _true_type has_trivial_assignment_operator;
          typedef _true_type has_trivial_destructor;
          typedef _true_type is_POD_type;
      };
      
      /*
      其他類型類推定義偏特化版本。
      */
    
      // 判斷元素是否為non trivaial destructor。激活重載決議
      template <typename ForwardIterator>
      inline void destory(ForwardIterator first, ForwardIterator last) {
          /* 
          利用_type_traits類嵌型別萃取
          */
          typedef typename _type_traits<ForwardIterator>::is_POD_type is_POD_type;
          __destory_aux(first, last, is_POD_type());
      }
    
      // 元素是trivial destructor, 則什么都不用做
      template <typename ForwardIterator>
      inline void __destory_aux(ForwardIterator first, ForwardIterator last,    
      _true_type) {
      }
     
      // 元素是non trivial destrouctor,調(diào)用destroy
      template <typename ForwardIterator>
      inline void __destory_aux(ForwardIterator first, ForwardIterator last,    
      _false_type) {
          for (; first != last; ++first)
              destory(&*first);
      }
    
      // destroy中調(diào)用析構(gòu)函數(shù)
      template <typename T>
      inline void destory(T* pointer) {
          pointer->~T();
      }
    
  • 迭代器中的iterator_traits
    見下一篇文章迭代器。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容