C++Template(类模板二)

namespace _myspace
{
template<typename T, typename U>
class TC
{
public:
TC()
{
cout << "TC()的泛化版本" << endl;
}
void testone()
{
cout << "泛化版本函数:void testone" << endl;
}
};
}
int main(int argc,char** argv[])
{
_myspace::TC<int,float> mytcone;
mytcone.testone();


while (1);
return 0;
}

输出

 

 

 

 

 

 

上面的代码毋庸置疑执行泛化版本。

接下来改进:

加入全特化版本:

namespace _myspace
{
    template<typename T, typename U>
    class TC
    {
    public:
        TC()
        {
            cout << "TC()的泛化版本" << endl;
        }
        void testone()
        {
            cout << "泛化版本函数:void testone" << endl;
        }
    };
    template<>
    class TC<int, float>
    {
    public:
        TC()
        {
            cout << "TC()的全特化版本" << endl;
        }
        void testone()
        {
            cout << "全特化版本函数:void testone" << endl;
        }
    };
}
int main(int argc,char** argv[])
{
    _myspace::TC<int,float> mytcone;
    mytcone.testone();


    while (1);
    return 0;
}

输出:

 

 

 

接下来加入偏特化版本:

namespace _myspace
{
    template<typename T, typename U>
    class TC
    {
    public:
        TC()
        {
            cout << "TC()的泛化版本" << endl;
        }
        void testone()
        {
            cout << "泛化版本函数:void testone" << endl;
        }
    };
    template<>
    class TC<int, float>
    {
    public:
        TC()
        {
            cout << "TC()的全特化版本" << endl;
        }
        void testone()
        {
            cout << "全特化版本函数:void testone" << endl;
        }
    };

    template<typename U>
    class TC<float, U>
    {
    public:
        TC()
        {
            cout << "TC()的偏特化版本" << endl;
        }
        void testone()
        {
            cout << "偏特化版本函数:void testone" << endl;
        }
    };

}
int main(int argc,char** argv[])
{
    _myspace::TC<float,float> mytcone;
    mytcone.testone();


    while (1);
    return 0;
}

输出:

 

 

 

以上是对模板参数数量的特化,接下来是模板参数范围的偏特化:

比如int向const int,T向T*转型类型的范围就变小了,所以这种方式也可以进行特化:

namespace _myspace
{
    template<typename T, typename U>
    class TC
    {
    public:
        TC()
        {
            cout << "TC()的泛化版本" << endl;
        }
        void testone()
        {
            cout << "泛化版本函数:void testone" << endl;
        }
    };
    template<>
    class TC<int, float>
    {
    public:
        TC()
        {
            cout << "TC()的全特化版本" << endl;
        }
        void testone()
        {
            cout << "全特化版本函数:void testone" << endl;
        }
    };

    template<typename U>
    class TC<float, U>
    {
    public:
        TC()
        {
            cout << "TC()的偏特化版本" << endl;
        }
        void testone()
        {
            cout << "偏特化版本函数:void testone" << endl;
        }
    };
    template<typename T,typename U>
    struct TC<const T, U*>
    {
        TC()
        {
            cout << "TC(const T,U*)" << endl;
        }
        void testone()
        {
            cout << "TC(const T,U*):void testone" << endl;
        }
    };

}
int main(int argc,char** argv[])
{
    _myspace::TC<const int,float*> mytcone;
    mytcone.testone();


    while (1);
    return 0;
}

输出:

 

 

以上是对类的特化,接下来谈一些细节的东西:

回到以上代码:

我对成员函数进行特化:

 

 

输出:

 

 

如果加入静态函数:

 

 

输出:

 

 

注意对于以上的成员函数的特化和加入了静态函数,那么就不能对类进行相对应的特化和偏特化:

比如下面这样就不行:

 

 

 

 

 

 

以上时注意点,最后,如果想要在类外定义相应的函数,那么template可以不用写,当成实例化的类看待即可:

如下图:

 

posted @ 2020-09-17 10:34  sunshine_gzw  阅读(201)  评论(0编辑  收藏  举报