mardi 20 avril 2021

Why does libc++'s implementation of shared_ptr use a unique_ptr to hold the raw pointer in the raw-pointer constructor?

In the raw-pointer constructor of shared_ptr, a unique_ptr __hold is used to hold the argument __p and then released without any visit? What's the usage of it?

template<class _Yp, class = _EnableIf< /*...*/ > >
explicit shared_ptr(_Yp* __p) : __ptr_(__p) {
    unique_ptr<_Yp> __hold(__p); // <--- created
    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
    typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk;
    __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT());
    __hold.release();  // <--- released
    __enable_weak_this(__p, __p);
}

Aucun commentaire:

Enregistrer un commentaire