c++新特性11 (10)shared_ptr六”构造函数unique_ptr参数“
生活随笔
收集整理的這篇文章主要介紹了
c++新特性11 (10)shared_ptr六”构造函数unique_ptr参数“
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 構造函數
shared_ptr p(u)
p從unique_ptr u中接管了對象的所有權;將u置為空
template <class _Ux, class _Dx,enable_if_t<conjunction_v<_SP_pointer_compatible<_Ux, _Ty>,is_convertible<typename unique_ptr<_Ux, _Dx>::pointer, element_type*>>,int> = 0>shared_ptr(unique_ptr<_Ux, _Dx>&& _Other) {using _Fancy_t = typename unique_ptr<_Ux, _Dx>::pointer;using _Raw_t = typename unique_ptr<_Ux, _Dx>::element_type*;using _Deleter_t = conditional_t<is_reference_v<_Dx>, decltype(_STD ref(_Other.get_deleter())), _Dx>;const _Fancy_t _Fancy = _Other.get();if (_Fancy) {const _Raw_t _Raw = _Fancy;const auto _Rx = new _Ref_count_resource<_Fancy_t, _Deleter_t>(_Fancy, _Other.get_deleter());_Set_ptr_rep_and_enable_shared(_Raw, _Rx);_Other.release();//將u置為空}}1.1 _Ref_count_resource
// CLASS TEMPLATE _Ref_count_resource template <class _Resource, class _Dx> class _Ref_count_resource : public _Ref_count_base { // handle reference counting for object with deleter public:_Ref_count_resource(_Resource _Px, _Dx _Dt): _Ref_count_base(), _Mypair(_One_then_variadic_args_t{}, _STD move(_Dt), _Px) {}#ifdef __EDG__ // TRANSITION, VSO-1292293virtual ~_Ref_count_resource() noexcept override {} // TRANSITION, should be non-virtual #else // ^^^ workaround / no workaround vvvvirtual ~_Ref_count_resource() noexcept override = default; // TRANSITION, should be non-virtual #endif // ^^^ no workaround ^^^virtual void* _Get_deleter(const type_info& _Typeid) const noexcept override { #if _HAS_STATIC_RTTIif (_Typeid == typeid(_Dx)) {return const_cast<_Dx*>(_STD addressof(_Mypair._Get_first()));} #else // _HAS_STATIC_RTTI(void) _Typeid; #endif // _HAS_STATIC_RTTIreturn nullptr;}private:virtual void _Destroy() noexcept override { // destroy managed resource_Mypair._Get_first()(_Mypair._Myval2);}virtual void _Delete_this() noexcept override { // destroy selfdelete this;}_Compressed_pair<_Dx, _Resource> _Mypair; };總結
以上是生活随笔為你收集整理的c++新特性11 (10)shared_ptr六”构造函数unique_ptr参数“的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++新特性11 (9)智能指针一”_C
- 下一篇: c++新特性11 (12)weak_pt