函数模版定义:
函数模版是通用的函数描述,有时候也被称为参数化类型。
建立模版如下:
template <class Any>
void Swap (Any&a,Any&b){
Any temp;
temp = a;
a=b;
b =temp;
}
#include <iostream> template <typename Any> void Swap(Any& a,Any& b); struct job { char name[40]; double salary; int floor; }; template <> void Swap<job>(job& job1,job& job2); void Show(job &j); int main() { using namespace std; cout.precision(2); cout.setf(ios::fixed,ios::floatfield); int i= 10, j = 20; cout<<"i,j"<<i<<","<<j<<endl; cout<<"Using compiler-generated int swapper: "<<endl; Swap(i,j); cout<<"Now i,j = "<<i<<","<<j<<endl; job sue = {"Susan",74000.60,7}; job sidney = {"Sidney",78060.72,9}; cout<<"Before job swapping:"<<endl; Show(sue); Show(sidney); Swap(sue,sidney); cout<<"After job swapping:"<<endl; Show(sue); Show(sidney); system("pause"); return 0; } template <typename Any> void Swap(Any& a,Any& b) { Any temp =a; a = b; b = temp; } template<> void Swap<job>(job& job1,job& job2) { double t1; int t2; t1 = job1.salary; job1.salary = job2.salary; job2.salary = t1; t2 = job1.floor; job1.floor = job2.floor; job2.floor = t2; } void Show(job &j) { using namespace std; cout<<j.name<<":$"<<j.salary<<" on floor "<<j.floor<<endl; }
#include <iostream> template <class Any>//定义一个模版(用class) void Swap(Any& a,Any& b);//定义一个模版函数 template <typename Any>//定义一个模版(用typename) void Swap(Any& a,Any& b,int c); void show(int a[]); const int Lim = 8; int main() { using namespace std; int i =0 ,j = 20; cout<<"i,j= "<<i<<","<<j<<endl; cout<<"using compiler-generated int Swapper"<<endl; Swap(i,j); cout<<"Now i,j = "<<i<<","<<j<<endl; int d1[Lim] = {0,7,0,4,1,7,7,6}; int d2[Lim] = {0,6,2,0,1,9,6,9}; cout<<"Original arrays:"<<endl; show(d1); show(d2); system("pause"); return 0; } template <typename Any> void Swap(Any& a,Any& b) { Any temp; temp = a; a= b; b=temp; } template <typename Any> void Swap(Any& a,Any& b,int c) { for (int i =0 ;i<c;i++) { Any temp = a[i]; a[i] = b[i]; b[i] = temp; } } void show(int a[]) { using namespace std; cout<<a[0]<<a[1]<<"/"; cout<<a[2]<<a[3]<<"/"; for (int i = 4;i<Lim;i++) { cout<<a[i]; } cout<<endl; }
显示具体化
1.对于给定的函数名,可以有非模版函数,正常模版函数和显示具体化以及它们的重载函数
2.显示具体化的原型和定义应以template<>开头,并通过名称制定类型
3.具体化将覆盖常规模版,而非模版函数覆盖,具体化和常规模版
#include <iostream> template <typename Any> void Swap(Any& a,Any& b); struct job { char name[40]; double salary; int floor; }; template <> void Swap<job>(job& job1,job& job2);//显示具体化声明 void Show(job &j); int main() { using namespace std; cout.precision(2); cout.setf(ios::fixed,ios::floatfield); int i= 10, j = 20; cout<<"i,j"<<i<<","<<j<<endl; cout<<"Using compiler-generated int swapper: "<<endl; Swap(i,j); cout<<"Now i,j = "<<i<<","<<j<<endl; job sue = {"Susan",74000.60,7}; job sidney = {"Sidney",78060.72,9}; cout<<"Before job swapping:"<<endl; Show(sue); Show(sidney); Swap(sue,sidney); cout<<"After job swapping:"<<endl; Show(sue); Show(sidney); system("pause"); return 0; } template <typename Any> void Swap(Any& a,Any& b) { Any temp =a; a = b; b = temp; } template<> void Swap<job>(job& job1,job& job2)//显示具体化定义(不需要再声明template <typename Any>) { double t1; int t2; t1 = job1.salary; job1.salary = job2.salary; job2.salary = t1; t2 = job1.floor; job1.floor = job2.floor; job2.floor = t2; } void Show(job &j) { using namespace std; cout<<j.name<<":$"<<j.salary<<" on floor "<<j.floor<<endl; }
显示具体化两种等价声明:
template<>void Swap<int>(int &,int&); ==template<> void Swap(int &,int&);
实例化和具体化
在代码中包含函数模版本身并不会生成函数定义,它只是一个用于函数定义的方案。编译器使用模版为特定类型生成函数定义时,得到的是模版实例。
两种实例化:隐式实例化和显示实例化。
句法是:template void Swap<int>(int,int)
注意:试图在同一编程单元中使用同一类型的显示实例和显示具体化将出错
隐式实例化,显示实例化和显示具体化统称为具体化。
相同之处在于它们表示的都是使用具体类型的函数定义,而不是通用描述
编译器选择使用哪个函数版本
1.创建候选函数列表。其中包含与被调函数函数名称相同的函数和模版函数
2.使用候选函数列表创建可行函数列表。(有一个隐式转换序列)
3.确定是否有最佳的可行函数