- //<stl_construct.h>的部分内容
-
-
#include <new.h>
-
-
template <typename T1,typename T2>
-
inline void construct (T1 *p, const T2& value)
-
{
-
new(p) T1(value);//调用T1::T1(value);
-
}
-
-
template <typename T>
-
inline void destroy (T * pointer)
-
{
-
pointer->~T();//调用~T();最终都调用这个版本的.
-
}
-
-
template <typename ForwardIterator>
-
inline void destory (ForwardIterator first, ForwardIterator last)
-
{
-
_destroy(first, last, value_type(first)); //调用_destroy,还传进元素的类型
-
}
-
-
template <typename ForwardIterator,typename T>
-
inline _destroy(ForwardIterator first, ForwardIterator last, T*)
-
{
-
typedef typename _type_trait<T>::has_trivial_destructor trivial_destructor;
-
_destroy_aux(first,last,trivial_destructor());//调用_destory_aux,判断是否有trivial destructor
-
}//trivial destructor 语义可以看Inside The C++ Object Model
-
-
template <typename ForwardIterator>
-
inline void _destory_aux (ForwardIterator first, ForwardIterator last, _true_type)
-
{
-
//如果有 trivial destructor 就什么也不做.
-
}
-
-
template <typename ForwardIterator>// 没有trivial destructor,逐个destroy
-
inline void _destory_aux (ForwardIterator first, ForwardIterator last, _false_type)
-
{
-
for ( ;first < last; ++first)
-
destroy(&*first);
-
}
-
-
inline void destroy( char* ,char *) {}//对char跟wchar_t的特化版
-
inline void destroy(wchar_t *,wchar_t *){}
//知道有这玩意之后就不会在CSDN看着他们争那题会不会内存泄漏了,虽然标准说行为未定义.但是真的实现怎么实现的后//就能对自己写的代码的行为有更加好的掌控,