• 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可以不用写,当成实例化的类看待即可:

    如下图:

  • 相关阅读:
    如何上传整个项目或者是文件夹到github
    阅读笔记16-架构师推荐:提高90%开发效率的工具推荐
    阅读笔记15-这些普通的程序猿,如今都已进阶成为技术大佬
    python爬虫——爬取淘票票正在热映电影
    阅读笔记12-Java 面试题 —— 老田的蚂蚁金服面试经历
    SOA架构设计案例分析
    阅读笔记11-孤独后厂村:30万互联网人跳不出的中国硅谷
    阅读笔记09-Java程序员必备的Intellij插件
    阅读笔记08-程序员依然是这个时代,贫寒学子翻身的不二选择
    os.path.join()
  • 原文地址:https://www.cnblogs.com/SunShine-gzw/p/13683766.html
Copyright © 2020-2023  润新知