SGL STL 中虽然有标准的allocate但是实际中使用的确是alloc
它们的主要区别就是alloc 不和allocate一样只是对new和delete进行封装。
new算式其实含有两段操作,1.配置内存,2构造队形。delete:1.析构对象,2.释放内存
在alloc就把中就把这两段操作分开为stl_construct.h(构造对象)和stl_alloc.h(分配内存)
下面就是stl_construct.h
//SGL 中的特殊的空间适配器 alloc
//把配置的内存和构造对象分开了
//分为了 <stl_construct.h>,<stl_alloc.h>,<stl_uninitialized.h>
//此为<stl_construct.h>
#include<new>
template <typename T1, typename T2>
inline void construct (T1 * p, const T2 & value) //构造元素
{
new (p) T1(value);
}
template <typename T>
inline void destroy (T * pointer)
{
pointer->~T();//显示调用T 的析构函数
}
template<typename ForwardIterator>
inline void destroy (ForwardIterator first, ForwardIterator last)//接受两个迭代器,设法找出元素类型
{
_destroy(first,last,value_type(first));
}
template <typename ForwardIterator>
inline void _destroy_aux(ForwardIterator first , ForwardIterator last , __false_type) //若销毁元素时元素的析构函数有意义则调用此函数
{
for(; first < last ; ++first)
{
destroy(&*first);
}
}
template <typename ForwardIterator>
inline void _destroy_aux( ForwardIterator , ForwardIterator , __true_type)//重载的_destroy_aux若元素没有有意义的析构则调用此函数
{}
inline void destroy (char * ,char *){}
inline void destroy (wchar_t * , wchar_t *){}
这其中的value_type()和__type_traits 就是判断元素类型这个以后再说.
这样做的主要目的就是为了避免有的元素的析构元素无用而我们还浪费时间的去调用他们。