模板全特化和偏特化用法

#include <iostream>

template <typename T, typename U>
class TC
{
public:
    TC()
    {
        std::cout << "泛化版本构造函数" << std::endl;
    }
    void funtest()
    {
        std::cout << "泛化版本成员函数" << std::endl;
    }

    void display()
    {
        std::cout << "泛化版本成员函数" << std::endl;
    }
};

template<>
class TC<int, int>
{
public:
    TC()
    {
        std::cout << "全特化版本构造函数" << std::endl;
    }
    void funtest()
    {
        std::cout << "全特化版本成员函数" << std::endl;
    }

};

//函数全特化
template<>
void TC<double, double>::funtest()
{
    std::cout << "全特化版本函数" << std::endl;
}

template<>
void TC<float, float>::display()
{
    std::cout << "全特化版本函数" << std::endl;
}


int main()
{
    TC<int, int> tint;
    tint.funtest();

    TC<float, float>ints;
    ints.display();
}

猜你喜欢

转载自blog.csdn.net/qq_53332653/article/details/115352368