浅析shared_ptr的enable_shared_from_this

浅析shared_ptr的enable_shared_from_this

背景

考虑以下场景,当一个对象被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传递出去,解决异步回调传参问题。

<think>我们正在讨论C++中的`std::enable_shared_from_this`模板类。根据用户的要求,我们需要介绍其作用、原理和使用方法。同时,我们需要遵循系统级指令,包括使用正确的LaTeX格式、中文回答、生成相关问题,并在引用末尾添加引用标识。 根据提供的引用内容: -引用[1]提到:使用`enable_shared_from_this`必须建立在对象本身被`shared_ptr`管理的前提下,否则会抛出`std::bad_weak_ptr`异常(从C++17开始)。其实现原理与`weak_ptr`密切相关。-引用[2]提到:`enable_shared_from_this`允许对象通过`shared_from_this()`成员函数获得指向自身的`shared_ptr`,用于解决多线程中的悬空指针问题。-引用[3]提到:`enable_shared_from_this`的主要设计目的是解决在类的成员函数中安全地创建`std::shared_ptr`实例的问题,避免两个常见问题(具体问题将在回答中说明)。 因此,我们的回答将围绕这些点展开。 ###回答结构:1.**作用**:解释为什么需要`enable_shared_from_this`,它解决了什么问题。2.**原理**:简要说明其内部实现机制(涉及`weak_ptr`)。 3. **使用方法**:展示如何正确使用这个模板类,并强调注意事项(特别是对象必须由`shared_ptr`管理)。4.**注意事项**:包括异常情况和错误用法。同时,我们将按照要求生成相关问题。###注意事项: -使用`$...$`表示行内数学表达式,但在这个主题中可能数学表达式较少,主要涉及代码和概念。 -独立公式使用`$$...$$`,但同样可能较少。-使用中文回答。 下面开始正式回答。</think>### C++ `enable_shared_from_this` 原理与使用详解 #### **作用与核心问题** `std::enable_shared_from_this` 是 C++ 标准库提供的模板类,用于解决**在对象内部安全获取指向自身的 `shared_ptr`** 的问题。主要避免以下两种错误场景: 1. **多个独立 `shared_ptr` 管理同一对象** 若在类成员函数中直接通过 `this` 创建新 `shared_ptr`(如 `shared_ptr<T>(this)`),会导致多个独立控制块存在,引发重复析构(双释放): ```cpp class Widget { public: void error() { auto sp = std::shared_ptr<Widget>(this); // 危险!独立控制块 } }; ``` 2. **返回裸指针导致悬空引用** 若返回 `this` 裸指针,外部可能将其转换为 `shared_ptr`,同样引发控制块冲突[^3]。 #### **实现原理** `enable_shared_from_this` 的核心依赖 `weak_ptr` 实现: 1. **内部维护 `weak_ptr`** 类继承 `enable_shared_from_this` 后,内部会隐式包含一个 `std::weak_ptr` 成员(通常命名为 `weak_this`)。 2. **构造时初始化 `weak_ptr`** 当对象被 `shared_ptr` 管理时,`shared_ptr` 构造函数会检测基类是否为 `enable_shared_from_this`。若是,则用当前 `shared_ptr` 初始化内部的 `weak_this`。 $$ \text{shared\_ptr<T>::shared\_ptr(T* ptr)} \rightarrow \text{若 T 继承 enable\_shared\_from\_this,则 weak\_this = *this} $$ 3. **`shared_from_this()` 工作流程** ```cpp shared_ptr<T> shared_from_this() { return weak_this.lock(); // 转为 shared_ptr(引用计数增加) } ``` 此操作要求 `weak_this` 已初始化,否则抛出 `std::bad_weak_ptr`[^1]。 #### **正确使用方法** 1. **继承模板类** ```cpp class MyClass : public std::enable_shared_from_this<MyClass> { public: std::shared_ptr<MyClass> get_ptr() { return shared_from_this(); // 安全获取 } }; ``` 2. **对象必须由 `shared_ptr` 管理** ```cpp // 正确用法 auto obj = std::make_shared<MyClass>(); auto sp = obj->get_ptr(); // 引用计数变为 2 // 错误用法:未通过 shared_ptr 构造 MyClass raw_obj; raw_obj.get_ptr(); // 抛出 std::bad_weak_ptr ``` 3. **禁止在构造函数中调用** 构造函数执行时 `weak_this` 尚未初始化,调用 `shared_from_this()` 会导致未定义行为。 #### **典型应用场景** 1. **异步回调** 在多线程中传递自身智能指针,确保对象生命周期覆盖回调执行: ```cpp class Session : public enable_shared_from_this<Session> { void async_send() { auto self = shared_from_this(); io_service.post([self] { self->handle_send(); }); // 避免回调时对象被销毁 } }; ``` 2. **链式调用** ```cpp class Parser : public enable_shared_from_this<Parser> { shared_ptr<Parser> set_option(Option opt) { options_ = opt; return shared_from_this(); // 返回自身指针用于链式调用 } }; ``` #### **注意事项** - **前置条件**:对象必须已被 `shared_ptr` 管理,否则 `shared_from_this()` 抛异常(C++17 起)[^1]。 - **性能开销**:内部 `weak_ptr` 带来少量内存和原子操作开销。 - **替代方案**:C++23 引入 `std::make_shared_from_this` 简化构造。 > 关键总结:`enable_shared_from_this` 通过 `weak_ptr` 桥接 `shared_ptr` 的控制块,实现安全共享自身所有权,是异步编程和资源管理的核心工具[^2][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值