zoukankan      html  css  js  c++  java
  • 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可以不用写,当成实例化的类看待即可:

    如下图:

  • 相关阅读:
    [pytorch][模型压缩] 通道裁剪后的模型设计——以MobileNet和ResNet为例
    [面试]DeeCamp2020面试记录
    [python][pytorch]多GPU下的模型保存与加载
    [目标检测][python][cpp]非极大值抑制(NMS)算法原理以及CPP实现
    [pytorch]动态调整学习率
    [pytorch][持续更新]pytorch踩坑汇总
    [CPP]push_back和emplace_back的区别
    [LeetCode in Python] 199 (M) binary tree right side view 二叉树的右视图
    [LeetCode in Python] 5390 (M) minimum number of frogs croaking 数青蛙
    [LeetCode in Python] 1345 (H) jump game iv 跳跃游戏 IV
  • 原文地址:https://www.cnblogs.com/SunShine-gzw/p/13683766.html
Copyright © 2011-2022 走看看