《stl源码剖析》 析构函数destroy分析
《stl源码剖析》 p51
//判断元素的数值型别,使用trivial destructor
template<class ForwardIterator,class T>
inline void __destroy(ForwardIterator first,ForwardIterator last,T*)
{
typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;
__destroy_aux(first,last,trivial_destructor());
}
//如果元素的数值型别(value type) 有non-trival destructor
template<class ForwardIterator>
inline void __destory_aux(ForwardIterator first,ForwardIterator last,false_type)
{
for(;first<last;++first)
destroy(&first);
}
//如果元素的数值型别(value type)有trivial destructor
template<class ForwardIterator>
inline void __destroy_aux(ForwardIterator,ForwardIterator,true_type){}
typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;
typedef __type_traits<T>::has_trivial_destructor trivial_destructor;
// TEMPLATE CLASS has_trivial_destructor
template<class _Ty>
struct has_trivial_destructor _HAS_TRIVIAL_DESTRUCTOR(_Ty)
{// determine whether _Ty has a trivial destructor
};
#define _HAS_TRIVIAL_DESTRUCTOR(_Ty)\
: _Cat_base<!is_void<_Ty>::value \
&& (is_pod<_Ty>::value || __has_trivial_destructor(_Ty))>
template<class _Ty>
struct has_trivial_destructor : _Cat_base<!is_void<_Ty>::value && (is_pod<_Ty>::value || __has_trivial_destructor(_Ty))>
{// determine whether _Ty has a trivial destructor
};
if(trivial_destructor())
{
for(;first<last;++first)
destroy(&first);
}
else
{
}
//如果元素的数值型别(value type) 有non-trival destructor
template<class ForwardIterator>
inline void __destory_aux(ForwardIterator first,ForwardIterator last,false_type)
{
for(;first<last;++first)
destroy(&first);
}
//如果元素的数值型别(value type)有trivial destructor
template<class ForwardIterator>
inline void __destroy_aux(ForwardIterator,ForwardIterator,true_type){}