c++新特性11 (10)shared_ptr三”创建计数器“
Posted thefist11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++新特性11 (10)shared_ptr三”创建计数器“相关的知识,希望对你有一定的参考价值。
1. 创建计数器_Ref_count类
构造函数中new
template <class _Ux,
enable_if_t<conjunction_v<conditional_t<is_array_v<_Ty>, _Can_array_delete<_Ux>, _Can_scalar_delete<_Ux>>, _SP_convertible<_Ux, _Ty>>, int> = 0>
explicit shared_ptr(_Ux* _Px) // construct shared_ptr object that owns _Px
if constexpr (is_array_v<_Ty>)
_Setpd(_Px, default_delete<_Ux[]>);
else
_Temporary_owner<_Ux> _Owner(_Px);
_Set_ptr_rep_and_enable_shared(_Owner._Ptr, new _Ref_count<_Ux>(_Owner._Ptr));
_Owner._Ptr = nullptr;
1.1 _Ref_count类
// CLASS TEMPLATE _Ref_count
template <class _Ty>
class _Ref_count : public _Ref_count_base // handle reference counting for pointer without deleter
public:
explicit _Ref_count(_Ty* _Px) : _Ref_count_base(), _Ptr(_Px)
private:
virtual void _Destroy() noexcept override // destroy managed resource
delete _Ptr;
virtual void _Delete_this() noexcept override // destroy self
delete this;
_Ty* _Ptr;
;
// CLASS TEMPLATE shared_ptr
template <class _Ux>
struct _Temporary_owner
_Ux* _Ptr;
explicit _Temporary_owner(_Ux* const _Ptr_) noexcept : _Ptr(_Ptr_)
_Temporary_owner(const _Temporary_owner&) = delete;
_Temporary_owner& operator=(const _Temporary_owner&) = delete;
~_Temporary_owner()
delete _Ptr;
;
1.2 _SP_convertible
// TYPE TRAIT _SP_convertible
template <class _Yty, class _Ty> struct _SP_convertible : is_convertible<_Yty*, _Ty*>::type ;
template <class _Yty, class _Uty>
struct _SP_convertible<_Yty, _Uty[]> : is_convertible<_Yty (*)[], _Uty (*)[]>::type ;
template <class _Yty, class _Uty, size_t _Ext> struct _SP_convertible<_Yty, _Uty[_Ext]> : is_convertible<_Yty (*)[_Ext], _Uty (*)[_Ext]>::type ;
is_convertible是一个编译器内部支持的函数,能够获取到两个类之间是否能够转换
以上是关于c++新特性11 (10)shared_ptr三”创建计数器“的主要内容,如果未能解决你的问题,请参考以下文章
c++新特性11 (10)shared_ptr八”shared_ptr类“
c++新特性11 (10)shared_ptr四”_Ptr_base 类“
c++新特性11 (10)shared_ptr五”构造函数“