c++ - 模板友元函数重载

标签 c++ c++11

我正在阅读 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/

相关文章:

c++ - 为什么不重新锁定互斥锁的 condition_variable 没有等待函数

c++ - std::is_const 将 const 指针标识为非常量

c++ - 我可以同时复制一个 shared_ptr 吗?

c++ - 从注册表中读取值 C++

c++ - 使用C++的就地字符串压缩问题

c++ - 为什么在析构函数中抛出异常时不调用重载删除?

c++ - 无序映射初始化失败

c++ - 静态变量是全局变量吗?

c++ - 本地类型作为未实例化函数内的模板参数

android - 超时后CPP调用函数