STL初探——构造和析构的基本工具: construct()和destroy()

  以下是这两个函数的整体示意图:

 

    

  construct()和destroy()函数必须被设计为全局函数,STL规定空间配置器必须包含这两个成员函数,但是std::alloc的配置器并没有遵循这一规则。

  以下是包含在<stl_construct.h>头文件中的两个函数的的定义:

  

/* 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<T>判断该型别的析构函数是否真的无关痛痒,是的话(__true_type),则什么也不做就结束,若不是(__false_type),那就通过循环的方式遍历整个范围一一调用destroy()函数进行析构。

  以前的C++98并不支持对 “指针所指之物” 的型别判断,也不支持 “对象析构函数是否为trivial” 的判断,现在的C++11新特性有个decltype,可以获取指针所指之物的型别,至于判断__type_traits<>,则需要另外设定对象的特性萃取方案,好在SGI STL里面有一道关于traits编程技法的私房菜,后面再写博客介绍。

转载于:https://www.cnblogs.com/Forever-Road/p/6800582.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值