• 实例化和具体化详解


    primer Plus在解释具体化和实例化看的有点乱,分解出来备忘

    在代码中包含函数模板本身并不会生成函数定义,它只是用于生成函数定义的方案

    编译器使用模板为我写类型生成函数定义时,得到的是模板实例

    如这个模板

    template<typename T>
    void Swap(T &t1,T &t2)
    {
        T _t;
        _t=t1;    
        t1=t2;
        t2=_t;
    }

    调用

        int i = 10,j=20;
        ::cout<<"i, j ="<<i<<" , "<<j<<endl;
        cout<<" Using compiler -generated int swapper:
    ";
        Swap(i,j);
        ::cout<<"Now i, j ="<<i<<" , "<<j<<endl;

    调用 Swap(i,j)导致编译器生成Swap()的一个实例,该实例使用int类型。模板并非函数定义,但使用int的模板实例是函数定义。
    这种实例化方式被称为隐式实例化,编译器之所以知道需要进行定义,是由于程序调用Swap()函数时提供了int 参数。

    c++还允许显示实例化

    其语法是,声明所需的种类用<>指示类型并在声明前加上template:

    template void Swap<int>(int &t1,int &t2);

    例子

    #include<iostream>
    using namespace std;
    
    template<typename T>
    void Swap(T &t1,T &t2);
    
    template void Swap<int>(int &t1,int &t2);
    
    int main()
    {
        cout<<" Using compiler -generated int swapper:
    ";
        int i = 10,j=20;
        cout<<"i, j ="<<i<<" , "<<j<<endl;    
        cout<<" Using compiler -generated int swapper:
    ";
        Swap(i,j);
        cout<<"Now i, j ="<<i<<" , "<<j<<endl;    
        cin.get();
    }
    
    template<typename T>
    void Swap(T &t1,T &t2)
    {
        T _t;
        _t=t1;    
        t1=t2;
        t2=_t;
    }


    显示具体化的原型和定义应以template<>打头,并通过名称来指出类型。

    显式具体化优先于常规模板,而非模板函数优先于具体化和常规模板

    与显式实例化不同的是,显式具体化使用下面的声明方式 ,两种方式是一样的

    template<> void Swap<job>(job &c1,job &c2);
    template<> void Swap(job &c1,job &c2);

    这们的意思是不要使用Swap()模板来生成函数定义,而使用专门的job类型显式地定义函数定义
    显式具体化声明在关键字template 后加<>,显式实例化没有

    具体化小例子

    #include<iostream>
    using namespace std;
    
    struct job
    {
        char name[40];
        double salary;
        int floor;
    };
    template<typename T>
    void Swap(T &t1,T &t2);
    template<> void Swap<job>(job &c1,job &c2);
    
    void show(job j);
    int main()
    {
        
    
        job sue={"Suan Yaffee",73000.123,7};
        job sidey={"Sidney Taffee",78060.1,2};
        cout<<"sue:
    ";
        show(sue);
        cout<<"sidey:
    ";
        show(sidey);
        Swap(sue,sidey);
        cout<<"sue:
    ";
        show(sue);
        cout<<"sidey:
    ";
        show(sidey);
        cin.get();
    }
    
    template<typename T>
    void Swap(T &t1,T &t2)
    {
        T _t;
        _t=t1;    
        t1=t2;
        t2=_t;
    }
    
    template<> void Swap<job>(job &j1,job &j2)
    {
        double t1;
        int t2;
        t1 = j1.salary;
        j1.salary=j2.salary;
        j2.salary=t1;
    
        t2=j1.floor;
        j1.floor=j2.floor;
        j2.floor=t2;
    }
    void show(job j)
    {
        cout<<"name :"<<j.name<<"  salary:  "<<j.salary<<" floor :  "<<j.floor<<endl;
    }
  • 相关阅读:
    POJ 1811 大整数素数判断 Miller_Rabin
    hdu 4940 无源汇有上下界最大流
    hdu 4975 最大流解决行列和求矩阵问题,用到矩阵dp优化
    hdu 4971 多校10最大权闭合图
    hdu 4888 最大流给出行列和求矩阵
    poj 3621最优比例生成环(01分数规划问题)
    poj 2728 最优比例生成树(01分数规划)模板
    最优比例生成树最优比率生成树 01分数规划问题
    poj 2553强连通+缩点
    poj 2831 次小生成树模板
  • 原文地址:https://www.cnblogs.com/li-peng/p/3479994.html
Copyright © 2020-2023  润新知