模板好友函数重载
template friend function overloading
过去两天我正在阅读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
。
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 可以打印矢量和矢量中的矢量的非重载C++函数
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 使用模板重载函数
- C++线程中,没有重载函数接受 X 参数
- std::vector 没有重载函数的实例与参数列表匹配
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 对重载函数find_first_not_of的不明确调用
- 如何从重载解析中删除重载函数?
- CUDA:重载函数"isnan"的多个实例
- C++派生类重载函数(带有 std::function 参数)不可见
- 避免在人为的重载函数调用中拼写出类型
- C++:如何为多个重载函数保留通用代码路径?
- 什么时候可以使用常量装饰调用我的重载函数?
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- std::调用,未找到匹配的重载函数
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 为什么使用不匹配的参数调用重载函数仍然有效
- 如何通过签名作为模板参数来解决重载函数?