(44)函 _Make_associated_state (Alloc& ), 本函数返回在堆区创建的 _Associated_state<_Ty> 对象 的地址。本函数用于promise 的源代码中:
// construct an _Associated_state object with an allocator 用分配器构造 _Associated_state 对象
template <class _Ty, class _Alloc>
_Associated_state<_Ty>* // 注意本函数的返回值即可,在堆区创建 _Associated_state<_Ty> 对象
_Make_associated_state(const _Alloc& _Al)
{
using _Delty = _State_deleter<_Ty, _Associated_state<_Ty>, _Alloc>;
using _Aldelty = _Rebind_alloc_t<_Alloc, _Delty>; //= _Alloc< _Delty > 凭经验
using _Alstate = _Rebind_alloc_t<_Alloc, _Associated_state<_Ty>>; //= _Alloc< _Associated_state<_Ty> >
_Aldelty _Del_alloc(_Al); // 产生一个删除器
_Alstate _State_alloc(_Al); // 用指定的分配器产生一个 _Associated_state<_Ty> 对象
auto _Del = _Make_unique_alloc(_Del_alloc, _Al); // _Del 和 _Res 是 unique_ptr 指针
auto _Res = _Make_unique_alloc(_State_alloc, _Unfancy(_Del.get()));
/*
template <class _Ty, class _Dx >
class unique_ptr { _Compressed_pair<_Dx, pointer> _Mypair;
pointer get() { return _Mypair._Myval2; }
pointer release() noexcept { return _STD exchange(_Mypair._Myval2, nullptr); }
};
*/
(void)_Del.release(); // ownership of _Del.get() now transferred to _Res
return _Unfancy(_Res.release()); // ownership transferred to caller
/*
template <class _Ptrty> // converts from a fancy pointer to a plain pointer
auto _Unfancy(_Ptrty _Ptr) noexcept { return _STD addressof(*_Ptr); } // 从智能指针转换为裸指针
template <class _Ty> // do nothing for plain pointers
_Ty* _Unfancy(_Ty* _Ptr) noexcept { return _Ptr; }
*/
}
(45)promise 的析构函数 ~promise ( ) 不会导致进程异常退出, 测试一下:
++ 测试 :
(46)
谢谢