2014-01-13 51 views
1

我在过去两天里正在阅读std::enable_shared_from_this(g ++版本)源代码。有两个问题让我困惑。
让我先显示简要的源代码。模板好友功能重载

template<typename _Tp> 
class enable_shared_from_this { 
... 
private: 
    template<typename _Tp1> 
    friend void 
    __enable_shared_from_this_helper(const __shared_count<>& __pn, 
     const enable_shared_from_this* __pe, 
     const _Tp1* __px) noexcept { 
     } 
}; 

template<typename _Tp1, typename _Tp2> 
void 
__enable_shared_from_this_helper(const __shared_count<>&, 
     const enable_shared_from_this<_Tp1>*, 
     const _Tp2*) noexcept; 

问题:
1.注意行const enable_shared_from_this* __pe,没有标记 '<>' enable_shared_from_this后,这是否意味着这里enable_shared_from_this<__Tp1>
2.这里有两个超载函数模板__enable_shared_from_this_helper,我的测试显示在class enable_shared_from_this中定义的版本会永远被调用,为什么?

谢谢你们,我将不胜感激。

回答

0

对于1 .:不,这意味着此处隐含<_Tp>,而不是附加模板参数中的<_Tp1>。如果在类的定义(或成员方法定义的主体)内省略类名的模板参数,并且期望类型,则隐含类本身的参数。

2:这与_Tp1而不是_Tp_Tp2而不是_Tp1只是第一个相同。