我正在阅读 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
中定义的版本总是会被调用,为什么?
谢谢你们,不胜感激。
最佳答案
对于 1.: 不,这意味着 <_Tp>
在这里是隐含的,而不是 <_Tp1>
来自额外的模板参数。当您在类的定义(或成员方法定义的主体)中省略类名的模板参数时,并且需要类型时,隐含来自类本身的参数。
对于 2.:这与第一个相同,只是带有 _Tp1
。而不是 _Tp
和 _Tp2
而不是 _Tp1
.
关于c++ - 模板友元函数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21084835/