Visual C++ 2015 下的 enable_shared_from_this 原理简析
2015-07-28 22:42
806 查看
一般来说,搞到一个
使用构造函数,如:
使用
在 Visual C++ 2015 下,第二种方式也走的是构造函数:
如果你去翻阅 Visual C++ 2015 下的
而
在
注意:
如果
如果没有,执行第二个(no-op)。
现在让我们来看看
需要注意的:
它有一个上文提到的
它有一个
也就是说,实际上上一个
哦,初始化了
shared_ptr有两种常见方式:
使用构造函数,如:
[code] std::shared_ptr<foo> ptr{new foo{}};
使用
make_shared(
allocate_shared),如:
[code] auto ptr = std::make_shared<foo>();
在 Visual C++ 2015 下,第二种方式也走的是构造函数:
[code] // TEMPLATE FUNCTION make_shared template<class _Ty, class... _Types> inline shared_ptr<_Ty> make_shared(_Types&&... _Args) { // make a shared_ptr _Ref_count_obj<_Ty> *_Rx = new _Ref_count_obj<_Ty>(_STD forward<_Types>(_Args)...); shared_ptr<_Ty> _Ret; _Ret._Resetp0(_Rx->_Getptr(), _Rx); return (_Ret); }
如果你去翻阅 Visual C++ 2015 下的
<memory>头文件,你会发现所有的 ctor 都将工作委托给某个叫做
_Resetp的函数:
[code]template<class _Ux> explicit shared_ptr(_Ux *_Px) { // construct shared_ptr object that owns _Px _Resetp(_Px); }
而
_Resetp又会把工作委托给
_Resetp0:
[code] template<class _Ux> void _Resetp0(_Ux *_Px, _Ref_count_base *_Rx) { // release resource and take ownership of _Px this->_Reset0(_Px, _Rx); _Enable_shared(_Px, _Rx); } };
在
_Resetp0中会调用
_Enable_shared函数:
[code]template<class _Ty> inline void _Enable_shared(_Ty *_Ptr, _Ref_count_base *_Refptr, typename _Ty::_EStype * = 0) { // reset internal weak pointer if (_Ptr) _Do_enable(_Ptr, (enable_shared_from_this<typename _Ty::_EStype>*)_Ptr, _Refptr); } inline void _Enable_shared(const volatile void *, const volatile void *) { // not derived from enable_shared_from_this; do nothing }
注意:
如果
_Ty有一个
_EStype,那么执行第一个;
如果没有,执行第二个(no-op)。
现在让我们来看看
enable_shared_from_this:
[code] template<class _Ty> class enable_shared_from_this { // provide member functions that create shared_ptr to this public: typedef _Ty _EStype; shared_ptr<_Ty> shared_from_this() { // return shared_ptr return (shared_ptr<_Ty>(_Wptr)); } shared_ptr<const _Ty> shared_from_this() const { // return shared_ptr return (shared_ptr<const _Ty>(_Wptr)); } protected: _CONST_FUN enable_shared_from_this() _NOEXCEPT { // construct (do nothing) } enable_shared_from_this(const enable_shared_from_this&) _NOEXCEPT { // construct (do nothing) } enable_shared_from_this& operator=(const enable_shared_from_this&) _NOEXCEPT { // assign (do nothing) return (*this); } ~enable_shared_from_this() _NOEXCEPT { // destroy (do nothing) } private: template<class _Ty1, class _Ty2> friend void _Do_enable( _Ty1 *, enable_shared_from_this<_Ty2>*, _Ref_count_base *); weak_ptr<_Ty> _Wptr; };
需要注意的:
它有一个上文提到的
_EStype;
它有一个
weak_ptr成员。
也就是说,实际上上一个
_Enable_shared函数遇到
enable_shared_from_this及其子类会执行第一个,其它的是 no-op。那么第一个干了什么?
[code] template<class _Ty1, class _Ty2> inline void _Do_enable( _Ty1 *_Ptr, enable_shared_from_this<_Ty2> *_Es, _Ref_count_base *_Refptr) { // reset internal weak pointer _Es->_Wptr._Resetw(_Ptr, _Refptr); }
哦,初始化了
enable_share_from_this的
weak_ptr。有了
weak_ptr,其
share_from_this无非就是:
[code]shared_ptr<_Ty> shared_from_this() { // return shared_ptr return (shared_ptr<_Ty>(_Wptr)); }
相关文章推荐
- C语言程序 指针
- C++构造函数的自动调用(调用一个父类的构造函数,有显性调用最好,否则就默认调用无参数的构造函数)——哲学思想:不调用怎么初始化父类的成员数据和VMT?
- C++新特性学习(3)
- C语言中关于位域的介绍
- C++新特性学习(4)
- C++新特性学习(2)
- C语言之函数调用16—递归法之一般函数的调用(1)
- C++对象内存布局(1)
- 猜数字游戏
- c++指针作为函数参数传递的问题
- C语言之函数调用15—递归法求哥德巴赫猜想
- C++ IO类
- C++ outline and interview questions (2): Pointers
- C语言知识总结
- C语言之函数调用14—递归法打印勒让德多项式前N项
- 给source insight添加.cc的C++文件后缀识别
- 使用C语言访问MySQL数据 —— 连接和错误处理
- iOS分分钟搞定C语言 —— 进制
- C语言之函数调用13—递归法求N阶勒让德多项式的值
- C/C++中的static关键字