c++新特性11 (12)weak_ptr类定义
生活随笔
收集整理的這篇文章主要介紹了
c++新特性11 (12)weak_ptr类定义
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 類定義
// CLASS TEMPLATE weak_ptr template <class _Ty> class weak_ptr : public _Ptr_base<_Ty> { // class for pointer to reference counted resource public:constexpr weak_ptr() noexcept {}weak_ptr(const weak_ptr& _Other) noexcept {this->_Weakly_construct_from(_Other); // same type, no conversion}template <class _Ty2, enable_if_t<_SP_pointer_compatible<_Ty2, _Ty>::value, int> = 0>weak_ptr(const shared_ptr<_Ty2>& _Other) noexcept {this->_Weakly_construct_from(_Other); // shared_ptr keeps resource alive during conversion}…
1.1 _Weakly_construct_from
自身的計數器要加1,基類 的weak計算器加1。
template <class _Ty2>void _Weakly_construct_from(const _Ptr_base<_Ty2>& _Other) noexcept { // implement weak_ptr's ctorsif (_Other._Rep) {_Ptr = _Other._Ptr;_Rep = _Other._Rep;_Rep->_Incwref();} else {_STL_INTERNAL_CHECK(!_Ptr && !_Rep);}}注,基類的兩個計數器:
_Atomic_counter_t _Uses = 1;
_Atomic_counter_t _Weaks = 1;
總結
以上是生活随笔為你收集整理的c++新特性11 (12)weak_ptr类定义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++新特性11 (10)shared_
- 下一篇: c++新特性11 (11)unique_