以下是这两个函数的整体示意图:
construct()和destroy()函数必须被设计为全局函数,STL规定空间配置器必须包含这两个成员函数,但是std::alloc的配置器并没有遵循这一规则。
以下是包含在
/* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/
#ifndef __SGI_STL_INTERNAL_CONSTRUCT_H
#define __SGI_STL_INTERNAL_CONSTRUCT_H
#include <new.h>
__STL_BEGIN_NAMESPACE
// construct and destroy. These functions are not part of the C++ standard,
// and are provided for backward compatibility with the HP STL. We also
// provide internal names _Construct and _Destroy that can be used within
// the library, so that standard-conforming pieces don't have to rely on
// non-standard extensions.
// Internal names
template <class _T1, class _T2>
inline void _Construct(_T1* __p, const _T2& __value)
{
new ((void*) __p) _T1(__value);
}
template <class _T1>
inline void _Construct(_T1* __p)
{
new ((void*) __p) _T1();
}
//第一版本,接受一个指针,好说,直接调用析构函数全部析构
template <class _Tp>
inline void _Destroy(_Tp* __pointer)
{
__pointer->~_Tp();
}
//以下是destroy()第二版本,接受两个迭代器,只删除一部分值,该函数设法找出元素的数值型别,进而利用__type_traits<>求取最适当措施
template <class _ForwardIterator>
inline void _Destroy(_ForwardIterator __first, _ForwardIterator __last) {
__destroy(__first, __last, __VALUE_TYPE(__first));
}
//判断元素的数值型别(value_type)是否有trivial destructor
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());
}
//如果元素型别为non-trivial destructor(有意义),逐一删除元素
template <class _ForwardIterator>
void
__destroy_aux(_ForwardIterator __first, _ForwardIterator __last, __false_type)
{
for ( ; __first != __last; ++__first)
destroy(&*__first);
}
//如果元素型别为trivial destructor(无伤大雅),什么也不做
template <class _ForwardIterator>
inline void __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type) {}
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*) {}
#endif /* __STL_HAS_WCHAR_T */
// --------------------------------------------------
// Old names from the HP STL.
//HP STL以前旧的函数命名
template <class _T1, class _T2>
inline void construct(_T1* __p, const _T2& __value)
{
_Construct(__p, __value);
}
template <class _T1>
inline void construct(_T1* __p)
{
_Construct(__p);
}
template <class _Tp>
inline void destroy(_Tp* __pointer)
{
_Destroy(__pointer);
}
template <class _ForwardIterator>
inline void destroy(_ForwardIterator __first, _ForwardIterator __last)
{
_Destroy(__first, __last);
}
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_CONSTRUCT_H */
// Local Variables:
// mode:C++
// End:
上述 _Construct(_T1* __p, const _T2& __value), 接受一个指针__p和一个初值__value,该函数的用途是将初值设定到指针所指的空间上,通过C++的placement new 运算子来完成。
destory()有两个版本,第一个版本接受一个指针,意思是要释放掉该指针所指的内存,直接调用析构函数就行,第二个版本接受 first 和 last 两个迭代器,意思是析构掉[first, last)范围内的所有对象,这里存在一个型别判断问题,当范围特别大时,而每个对象的析构函数都是trivial destructor(无关痛痒),那么一次次调用这样的析构函数实际上是对效率的一种伤害,因此就应该先利用value_type()判断迭代器所指对象的型别,再利用type_traits
以前的C++98并不支持对 “指针所指之物” 的型别判断,也不支持 “对象析构函数是否为trivial” 的判断,现在的C++11新特性有个decltype,可以获取指针所指之物的型别,至于判断__type_traits<>,则需要另外设定对象的特性萃取方案,好在SGI STL里面有一道关于traits编程技法的私房菜,后面再写博客介绍。
原文链接: https://www.cnblogs.com/Forever-Road/p/6800582.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/253494
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!