标签:stl内存分配器
template <class _T1, class _T2> inline void _Construct(_T1* __p, const _T2& __value) { new ((void*) __p) _T1(__value); placement new运算符:在已分配的内存上构造函数对象。opreator new ,new opreator,placement new区别请<a target=_blank href="http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html">Google</a> } template <class _T1> inline void _Construct(_T1* __p) { new ((void*) __p) _T1(); } template <class _Tp> inline void _Destroy(_Tp* __pointer) { __pointer->~_Tp(); } template <class _ForwardIterator> void __destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type) { for ( ; __first != __last; ++__first) destroy(&*__first); } template <class _ForwardIterator> inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {} template <class _ForwardIterator, class _Tp> inline void __destroy(_ForwardIterator __first, _ForwardIterator __last, _Tp*) { typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor; __destroy_aux(__first, __last, _Trivial_destructor()); } template <class _ForwardIterator> inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last) { __destroy(__first, __last, __VALUE_TYPE(__first)); }
inline void _Destroy(char*, char*) {} inline void _Destroy(int*, int*) {} inline void _Destroy(long*, long*) {} inline void _Destroy(float*, float*) {} inline void _Destroy(double*, double*) {} #ifdef __STL_HAS_WCHAR_T inline void _Destroy(wchar_t*, wchar_t*) {}
文件:stl_uninitialized.h 分析众多函数中的一个:uninitialized_copy 函数。 这个函数一共有三个版本:一个模板函数,一个char类型重载,一个wchar类型重载。具体请看。 char类型重载: inline char* uninitialized_copy(const char* __first, const char* __last,char* __result) { memmove(__result, __first, __last - __first); return __result + (__last - __first); } wchar类型重载: 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 _InputIter, class _ForwardIter> inline _ForwardIter uninitialized_copy(_InputIter __first, _InputIter __last, _ForwardIter __result) { return __uninitialized_copy(__first, __last, __result,__VALUE_TYPE(__result)); } template <class _InputIter, class _ForwardIter, class _Tp> inline _ForwardIter __uninitialized_copy(_InputIter __first, _InputIter __last,_ForwardIter __result, _Tp*) { typedef typename __type_traits<_Tp>::is_POD_type _Is_POD; return __uninitialized_copy_aux(__first, __last, __result, _Is_POD()); } template <class _InputIter, class _ForwardIter> inline _ForwardIter __uninitialized_copy_aux(_InputIter __first, _InputIter __last,_ForwardIter __result,__true_type) { return copy(__first, __last, __result); } template <class _InputIter, class _ForwardIter> _ForwardIter __uninitialized_copy_aux(_InputIter __first, _InputIter __last, _ForwardIter __result, __false_type) { _ForwardIter __cur = __result; __STL_TRY { for ( ; __first != __last; ++__first, ++__cur) _Construct(&*__cur, *__first); return __cur; } __STL_UNWIND(_Destroy(__result, __cur)); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:stl内存分配器
原文地址:http://blog.csdn.net/csdnjack_/article/details/48105091