stl读书笔记(2)- 模板特例化

本来这个不属于stl中的一个内容,但是我是stl中看到了这个。所以特此说出来,我不知道stl中在拷贝数据的时候,为什么要区分POD(plain old data)。我个人觉得应该是都可以的。

神马叫做模板特例化,举个简单的例子:

template<typename T0, typename T1>
 14 class CTest
 15 {
 16 public:
 17     void Print()
 18     {
 19         printf("Come in\n");
 20     }
 21 };
 22 
 23 template<typename T1>
 24 class CTest<T1,int>
 25 {
 26 public:
 27     void Print()
 28     {
 29         printf("Come in1\n");
 30     }
 31 };
上面进行了模板特例化,如果定义CTest<char *, int>,则选择的特例化的模板CTest<T1, int>。


如图是sgi_stl中使用了模板特例化,这样统一iterator_category, value_type, difference_type, pointer,reference类型。(我个人观点,我觉这个真的没啥用,不知道为什么要这样泛化性编程,难道支持不同的编译器?)

sgi_stl特例化真正的用途是为了区分POD类型,


__uninitialized_fill_aux实现调用如下:


如何区别调用上面不同的函数了?是通过__type_traits<_Tp>模板特例化来进行区别的,具体定义如下图:



template <class _Tp>
struct __type_traits { 
   typedef __true_type     this_dummy_member_must_be_first;
                   /* Do not remove this member. It informs a compiler which
                      automatically specializes __type_traits that this
                      __type_traits template is special. It just makes sure that
                      things work if an implementation is using a template
                      called __type_traits for something unrelated. */

   /* The following restrictions should be observed for the sake of
      compilers which automatically produce type specific specializations 
      of this class:
          - You may reorder the members below if you wish
          - You may remove any of the members below if you wish
          - You must not rename members without making the corresponding
            name change in the compiler
          - Members you add will be treated like regular members unless
            you add the appropriate support in the compiler. */
 

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



// Provide some specializations.  This is harmless for compilers that
//  have built-in __types_traits support, and essential for compilers
//  that don't.

#ifndef __STL_NO_BOOL

__STL_TEMPLATE_NULL struct __type_traits<bool> {
   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;
};

#endif /* __STL_NO_BOOL */

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

__STL_TEMPLATE_NULL struct __type_traits<signed 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;
};

__STL_TEMPLATE_NULL struct __type_traits<unsigned 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;
};

用来区分了POD类型,如果不属于POD类型,不能够直接调用memcpy函数,需要调用对象构造函数。(但是我觉得完全可以都调用构造函数,POD也可以认为是个“”类型“”,不知道到stl为啥要这么实现)


综上所述,我觉得stl中不要区别POD类型,有可能还有些细节东西没有明白。







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章