首页 > 代码库 > __type_traits(traits思想)--萃取型别

__type_traits(traits思想)--萃取型别

//type_traits.h----应用于STL内部,而非规范化内容//类似的,利用对象来特例化struct __true_type {};struct __false_type {};//设计榨取机template <class type>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;//是否默认构造函数---false表示采取非快速的方式   typedef __false_type    has_trivial_copy_constructor;//是否复制构造函数----- 需要用到construtor,destructor   typedef __false_type    has_trivial_assignment_operator;//是否分配空间函数--- 而不是malloc,memcpy   typedef __false_type    has_trivial_destructor;//是否注销函数   typedef __false_type    is_POD_type;//是否传统处理};//特化例子---都采取了快速方式了进行一下操作,拷贝,赋值__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;};//特化版本有://char ,signed char,unsigned char,short,unsigned short//int,unsigned int,long,unsigned long,float,double,long double//T* ,char*,signed char*,unsigned char*//-------------------------------------------------------------------------------------------------------//stl_uninitialized.h文件介绍全局函数//uninitialized_fill函数--赋值函数,初始化迭代器区间值template <class ForwardIterator, class Size, class T>inline ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n,                                            const T& x) {  return __uninitialized_fill_n(first, n, x, value_type(first));}//////具体实施函数是__uninitialized_fill_ntemplate <class ForwardIterator, class Size, class T, class T1>inline ForwardIterator __uninitialized_fill_n(ForwardIterator first, Size n,                                              const T& x, T1*) {  typedef typename __type_traits<T1>::is_POD_type is_POD;//可以看出是否具有特化版本,是则用,不是则用泛化 //泛化则会返回__false_type是一种安全保险的处理,调用了constructor之类的; //特化则会返回__true_type是一种高效的处理,因为表示该类型T1具有复制构造,默认构造,分配空间这行函数  return __uninitialized_fill_n_aux(first, n, x, is_POD());//此处利用类型对象,编译时就能确定调用哪个模板                                }//为__true_type的处理template <class ForwardIterator, class Size, class T>inline ForwardIterator__uninitialized_fill_n_aux(ForwardIterator first, Size n,                           const T& x, __true_type) {  return fill_n(first, n, x);}//此函数在stl_algobase.h文件中实现的。template <class OutputIterator, class Size, class T>OutputIterator fill_n(OutputIterator first, Size n, const T& value) {  for ( ; n > 0; --n, ++first)    *first = value;//就是个赋值的过程,显然这样可以交付给系统处理了  return first;}//为__false_type的处理template <class ForwardIterator, class Size, class T>ForwardIterator__uninitialized_fill_n_aux(ForwardIterator first, Size n,                           const T& x, __false_type) {  ForwardIterator cur = first;  __STL_TRY {//是tyr的宏    for ( ; n > 0; --n, ++cur)      construct(&*cur, x);//这是需要人为调用了系统的construct函数,在stl_construct.h文件中    return cur;  }  //如果没有完全成功,则把之前的成功全部释放  __STL_UNWIND(destroy(first, cur));//#define __STL_UNWIND(action) catch(...) { action; throw; }}//////具体实施函数是uninitialized_fill//template <class ForwardIterator, class T>inline void uninitialized_fill(ForwardIterator first, ForwardIterator last,                                const T& x) {  __uninitialized_fill(first, last, x, value_type(first));}template <class ForwardIterator, class T, class T1>inline void __uninitialized_fill(ForwardIterator first, ForwardIterator last,                                  const T& x, T1*) {  typedef typename __type_traits<T1>::is_POD_type is_POD;  __uninitialized_fill_aux(first, last, x, is_POD());                   }//truetemplate <class ForwardIterator, class T>inline void__uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,                          const T& x, __true_type){  fill(first, last, x);}//此函数在stl_algobase.h文件中实现的template <class ForwardIterator, class T>void fill(ForwardIterator first, ForwardIterator last, const T& value) {  for ( ; first != last; ++first)    *first = value;}//falsetemplate <class ForwardIterator, class T>void__uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,                          const T& x, __false_type){  ForwardIterator cur = first;  __STL_TRY {    for ( ; cur != last; ++cur)      construct(&*cur, x);  }  __STL_UNWIND(destroy(first, cur));}//////具体实施函数是uninitialized_copy////泛化template <class InputIterator, class ForwardIterator>inline ForwardIterator  uninitialized_copy(InputIterator first, InputIterator last,                     ForwardIterator result) {  return __uninitialized_copy(first, last, result, value_type(result));}//特化inline char* uninitialized_copy(const char* first, const char* last,                                char* result) {  memmove(result, first, last - first);  return result + (last - first);}//特化inline wchar_t* uninitialized_copy(const wchar_t* first, const wchar_t* last,                                   wchar_t* result) {  memmove(result, first, sizeof(wchar_t) * (last - first));  return result + (last - first);}template <class InputIterator, class ForwardIterator, class T>inline ForwardIterator__uninitialized_copy(InputIterator first, InputIterator last,                     ForwardIterator result, T*) {  typedef typename __type_traits<T>::is_POD_type is_POD;  return __uninitialized_copy_aux(first, last, result, is_POD());}//truetemplate <class InputIterator, class ForwardIterator>inline ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __true_type) {  return copy(first, last, result);//在stl_algobase.h实现}//falsetemplate <class InputIterator, class ForwardIterator>ForwardIterator __uninitialized_copy_aux(InputIterator first, InputIterator last,                         ForwardIterator result,                         __false_type) {  ForwardIterator cur = result;  __STL_TRY {    for ( ; first != last; ++first, ++cur)      construct(&*cur, *first);    return cur;  }  __STL_UNWIND(destroy(result, cur));}

__type_traits(traits思想)--萃取型别