首先shared_ptr大概包含以下数据单元:指向data field的element_type *类型的指针,以及一个间接的包含了_M_use_count,_M_weak_count的__shared_count(在某些情况下它可能还包含一个deletor对象和一个allocator对象,这一区域被称为control block,__shared_count中包含一个指向这个control block的指针)。 常规的std::...
_Sp_counted_ptr: 用于直接用裸指针对std::shared_ptr(_Yp* __p)进行初始化的情况,此时使用默认的deletor和allocator。 _Sp_counted_deleter: 用于同时指定裸指针和deletor/allocator的情况:std::shared_ptr(_Yp* __p, {delete p;})。 _Sp_counted_ptr_inplace: 用于通过std::make_shared() 或者std::a...
unique_ptr没有control block,deleter type(包括默认的std::default_delete)直接做在unique_ptr一起了...
#include <memory> std::shared_ptr<SingleObject> SingleObjectDeletor( SingleObject::GetInstancePtr(), [](SingleObject *){ SingleObject::GetInstance().DelObj(); }); SingleObject* SingleObject::msObj = NULL; SingleObject::SingleObject() { std::cout <<"This is the constructor of SignleObject...
shared_ptr类通常包含两个成员:一个T*(由operator->返回,并在operator*中解引用)和一个aux*,其中...
Interesting question on StackOverflow –Why does unique_ptr take two template parameters when shared_ptr only takes one? I haven’t used std::unique_ptr much and wasn’t aware that you could supply a custom deletor. If you provide the deleter as template argument (as in unique_ptr...
unique_ptr没有control block,deleter type(包括默认的std::default_delete)直接做在unique_ptr一起了...
std::shared_ptr<SingleObject> SingleObjectDeletor( SingleObject::GetInstancePtr(), [](SingleObject *){ SingleObject::GetInstance().DelObj(); }); SingleObject* SingleObject::msObj = NULL; SingleObject::SingleObject() { std::cout <<"This is the constructor of SignleObject class!\n"; ...
Deletor直接调用的就是`delete 类型`。不过是把undefined behavior给check了。shared_ptr实现的时候 直接把...
std::shared_ptr<SingleObject> SingleObjectDeletor( SingleObject::GetInstancePtr(), [](SingleObject *){ SingleObject::GetInstance().DelObj(); }); SingleObject* SingleObject::msObj = NULL; SingleObject::SingleObject() { std::cout <<"This is the constructor of SignleObject class!\n"; ...