背景
考虑以下场景,当一个对象被shared_ptr托管时,怎么从自身返回一个shared_ptr对象
struct A
{
public:
std::shared_ptr<A> GetSharedPtrFromThis()
{
return std::shared_ptr<A>(this);
}
};
class B
{
public:
void SetCallback(const std::shared_ptr<A>& spA)
{
......
}
};
std::shared_ptr<A> spA = std::make_shared<A>();
std::shared_ptr<B> spB = std::make_shared<B>();
spB->SetCallback(spA->GetSharedPtrFromThis());
因为B不知道A的生命周期,所以如果A提供裸指针,则可能存在crash风险(无法判断一个指针是否已经被释放),而直接用this来构造一个shared_ptr更是错误,原因是shared_ptr的指针和引用计数是分离的(多个shared_ptr指向同一块内存,但各自维护引用计数)。
解决方法
struct A : std::enable_shared_from_this<A>
{
public:
std::shared_ptr<A> GetSharedPtr()
{
return shared_from_this();
}
std::weak_ptr<A> GetWeakPtr()
{
return weak_from_this();
}
};
class B
{
public:
void SetCallback(const std::shared_ptr<A>& spA)
{
}
};
spB->SetCallback(spA->GetSharedPtr());
原理
enable_shared_from_this类内部持有weak_ptr
template<class _Ty>
class enable_shared_from_this
{ // provide member functions that create shared_ptr to this
public:
using _Esft_type = enable_shared_from_this;
......
mutable weak_ptr<_Ty> _Wptr;
};
并且这个weak_ptr是在对象构造的时候初始化的,shared_ptr的构造函数里会判断当前类是否可以enable_from_this
template<class _Yty,
class = void>
struct _Can_enable_shared
: false_type
{ // detect unambiguous and accessible inheritance from enable_shared_from_this
};
template<class _Other,
class _Yty>
void _Enable_shared_from_this1(const shared_ptr<_Other>& _This, _Yty * _Ptr, true_type)
{
if (_Ptr && _Ptr->_Wptr.expired())
{
// weak_ptr赋值
_Ptr->_Wptr = shared_ptr<remove_cv_t<_Yty>>(_This, const_cast<remove_cv_t<_Yty> *>(_Ptr));
}
}
template<class _Other,
class _Yty>
void _Enable_shared_from_this1(const shared_ptr<_Other>&, _Yty *, false_type)
{ // don't enable shared_from_this
}
template<class _Other,
class _Yty>
void _Enable_shared_from_this(const shared_ptr<_Other>& _This, _Yty * _Ptr)
{ // possibly enable shared_from_this
_Enable_shared_from_this1(_This, _Ptr, bool_constant<conjunction_v<
negation<is_array<_Other>>,
negation<is_volatile<_Yty>>,
_Can_enable_shared<_Yty>>>{});
}
_Can_enable_shared里判断是否能转换,如果是继承了enable_from_this,内部有定义_Esft_type,进而再确认是否能完成转换
template<class _Yty,
class = void>
struct _Can_enable_shared
: false_type
{ // detect unambiguous and accessible inheritance from enable_shared_from_this
};
template<class _Yty>
struct _Can_enable_shared<_Yty, void_t<typename _Yty::_Esft_type>>
: is_convertible<remove_cv_t<_Yty> *, typename _Yty::_Esft_type *>::type
{ // is_convertible is necessary to verify unambiguous inheritance
};
template<class _Ty>
class enable_shared_from_this
{ // provide member functions that create shared_ptr to this
public:
using _Esft_type = enable_shared_from_this;
总结
enable_shared_from_this类持有weak_ptr,并在shared_ptr构造时初始化。使用时再将此weak_ptr传递给外部或者利用weak_ptr再构造出shared_ptr传递出去,解决异步回调传参问题。