C++ 为什么在gcc/libstdc+中的u this helper的u enable u shared u中没有模板参数+;
我正在从这里阅读enable_shared_的stl代码,它位于gcc-4.9.2\libstdc++-v3\include\bits\shared_ptr.h中。然后我看到了:C++ 为什么在gcc/libstdc+中的u this helper的u enable u shared u中没有模板参数+;,c++,shared-ptr,libstdc++,C++,Shared Ptr,Libstdc++,我正在从这里阅读enable_shared_的stl代码,它位于gcc-4.9.2\libstdc++-v3\include\bits\shared_ptr.h中。然后我看到了: template<typename _Tp1> friend void __enable_shared_from_this_helper(const __shared_count<>& __pn, const enable_shared_from_thi
template<typename _Tp1>
friend void
__enable_shared_from_this_helper(const __shared_count<>& __pn,
const enable_shared_from_this* __pe,
const _Tp1* __px) noexcept
{
if (__pe != 0)
__pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
}
模板
朋友空虚
__从\u此\u帮助程序启用\u共享\u(常量\u共享\u计数和\u pn,
const enable\u shared\u来自\u this*\u pe,
常数Tp1*uuupx)无例外
{
如果(uu pe!=0)
__pe->弱赋值(常量转换(\uuuupx),\uuuupn);
}
我的问题是,为什么在这个*中没有const enable_shared_的模板参数?当shared\u ptr构造函数使用指向类a的指针从\u this\u helper调用\u enable\u shared\u时,它是如何工作的,其中a派生自\u this的enable\u shared\u?这是在
从\u this启用\u shared\u的定义中定义的。在类模板的定义中,模板名称也是注入的类名,可以无条件地用于引用正在实例化的任何专业。这称为注入的类名。C++标准允许这个例子,例如在14.6:
与普通(非模板)类一样,类模板具有
注入类名称(第9条)。可以使用注入的类名称
作为模板名或类型名。当它与
模板参数列表,作为模板的模板参数
模板参数,或作为详细说明类型中的最终标识符-
友元类模板声明的说明符,它引用
类模板本身否则,它相当于
模板名称后跟类的模板参数
随附的模板。
如果查看shared_ptr_base.h的源代码,您会发现模板参数是必需的,因为它是类之外的:
00730 // Friend of __enable_shared_from_this.
00731 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
00732 void
00733 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
00734 const __enable_shared_from_this<_Tp1,
00735 _Lp>*, const _Tp2*);
00473 template<typename _Tp>
00474 class enable_shared_from_this
00475 {
...
00502 template<typename _Tp1>
00503 friend void
00504 __enable_shared_from_this_helper(const __shared_count<>& __pn,
00505 const enable_shared_from_this* __pe,
00506 const _Tp1* __px)
00507 {
00508 if (__pe != 0)
00509 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
00510 }
00730//。
00731模板
00732无效
00733 \u从\u此\u帮助程序启用\u共享\u(常量\u共享\u计数&,
00734常数启用共享自常数Tp2*);
但是在shared_ptr.h中,定义是类内部的:
00730 // Friend of __enable_shared_from_this.
00731 template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
00732 void
00733 __enable_shared_from_this_helper(const __shared_count<_Lp>&,
00734 const __enable_shared_from_this<_Tp1,
00735 _Lp>*, const _Tp2*);
00473 template<typename _Tp>
00474 class enable_shared_from_this
00475 {
...
00502 template<typename _Tp1>
00503 friend void
00504 __enable_shared_from_this_helper(const __shared_count<>& __pn,
00505 const enable_shared_from_this* __pe,
00506 const _Tp1* __px)
00507 {
00508 if (__pe != 0)
00509 __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
00510 }
00473模板
00474类从\u中启用\u共享\u
00475 {
...
00502模板
00503朋友无效
00504 \uuuu从\u此\u帮助程序启用\u共享\u(常量\uuuu共享\u计数和\uuu pn,
00505常数从该*启用共享,
00506常数(Tp1*(px)
00507 {
00508如果(uu pe!=0)
00509 uuuu pe->u M uu弱u赋值(常数转换(uuu px),uuu pn);
00510 }