函数模板是C++新增的一种性质,它容许只定义一次函数的实现,便可使用不一样类型的参数来调用该函数。这样作能够减少代码的书写的复杂度,同时也便于修改(注:使用模板函数并不会减小最终可执行程序的大小,由于在调用模板函数时,编译器都根据调用时的参数类型进行了相应实例化)。下面来看看函数模板的使用过程:函数
[cpp] view plain copy.net
- struct job
- {
- char name[20];
- int salary;
- };
- template <class T> //函数模板声明,通用变量类型为T
- void swap(T &a, T &b);
- void showJob(const job &a);//打印job内容
- using std::cin;
- using std::cout;
- using std::endl;
- void main(void)
- {
- int a = 4;
- int b = 5;
- cout<<"Before swap a = "<<a<<" b="<<b<<endl;
- swap(a, b);
- cout<<"After swap a = "<<a<<" b="<<b<<endl;
- job jobA = {"coder", 10000};
- job jobB = {"manager", 1000};
- cout<<"Before swap";
- showJob(jobA);
- showJob(jobB);
- cout<<endl;
- swap(jobA, jobB);
- cout<<"After swap";
- showJob(jobA);
- showJob(jobB);
- cout<<endl;
- system("pause");
- };
- template<class T> //函数模板实现
- void swap(T &a, T &b)
- {
- T temp;
- temp = a;
- a = b;
- b = temp;
- }
- void showJob(const job &a)
- {
- cout<<" "<<a.name<<" = "<<a.salary;
- }
若是在上述job结构互换过程当中只想互换salary,而不换其余成员变量值那么怎么办呢?C++中能够经过如下几种方法来解决这一问题。code
1>显式具体化
显式具体化也是基于函数模板的,只不过在函数模板的基础上,添加一个专门针对特定类型的、实现方式不一样的具体化函数。blog
[cpp] view plain copyci
- template<>void swap<job>(job &a, job &b)
- {
- int salary;
- salary = a.salary;
- a.salary = b.salary;
- b.salary = salary;
- }
如上所示,该具体化函数的实现与模板并不一致,编译器解析函数调用时会选择最匹配的函数定义。get
2>定义同名常规函数编译器
[cpp] view plain copyit
- void swap(job &a, job &b)
- {
- int salary;
- salary = a.salary;
- a.salary = b.salary;
- b.salary = salary;
- }
因为编译器在重载解析时,会选择最匹配函数定义,因此在调用swap(jobA, jobB)时,编译器会选择void swap(job &a, job &b)函数定义,而屏蔽了模板函数。io
同时,模板函数也能够重载,其操做与常规函数一致。编译
[cpp] view plain copy
- template <class T> void swap(T &a, T &b);
- template <class T> void swap(T &a, T &b, T &c);
- template <typename T> void swap(T &a, T &b)
- {
- T temp;
- temp = a;
- a = b;
- b = temp;
- }
- template <typename T> void swap(T &a, T &b, T &c)
- {
- T temp;
- temp = a;
- a = b;
- b = c;
- c = temp;
- }
上面主要说的是函数模板的具体化,下面说下模板实例化。
函数模板:
[cpp] view plain copy
- #define MAXNAME 128
- struct job
- {
- char name[MAXNAME]:
- int salary;
- };
- template<class T>
- void swap(T &a, T &b )
- {
- T temp;
- temp = a;
- a = b;
- b = temp;
- };
- template void swap<int>(int &a, int & b); //显式实例化,只需声明
- template<> void swap<job>(job &a, job &b) //显式具体化(上面已经讲过,注意与实例化区分开,必须有定义)
- {
- int salary:
- salary = a.salary:
- a.salary = b.salary;
- b.salary = salary;
- };//explicite specialization.
类模板:
[cpp] view plain copy
- template <class T>
- class Arrary
- {
- private:
- T* ar;
- int l;
- ...
- };//template class declaration.
- template class Array<int>; //explicit instantiation. 显式实例化
- template<> class Array<job>
- {
- private:
- job* ar;
- int l;
- };//expicit specialization. 显式具体化,类定义体能够不一样于类模板Array
相应的,隐式实例化指的是:在使用模板以前,编译器不生成模板的声明和定义实例。只有当使用模板时,编译器才根据模板定义生成相应类型的实例。如:
int i=0, j=1;
swap(i, j); //编译器根据参数i,j的类型隐式地生成swap<int>(int &a, int &b)的函数定义。
Array<int> arVal;//编译器根据类型参数隐式地生成Array<int>类声明和类函数定义。
显式实例化:
当显式实例化模板时,在使用模板以前,编译器根据显式实例化指定的类型生成模板实例。如前面显示实例化(explicit instantiation)模板函数和模板类。其格式为:
template typename function<typename>(argulist);
template class classname<typename>;
显式实例化只需声明,不须要从新定义。编译器根据模板实现实例声明和实例定义。
显示具体化:
对于某些特殊类型,可能不适合模板实现,须要从新定义实现,此时可使用显示具体化(explicite specialization)。显示实例化需从新定义。格式为:
template<> typename function<typename>(argu_list){...};
template<> class classname<typename>{...};
综上:
template<> void swap<job>(job &a, job &b) {……};是函数模板的显式具体化,意思是job类型不适用于函数模板swap的定义,所以经过这个显式具体化从新定义;也可简写做template<> void swap(job &a, job &b);
template void swap<job>(job &a, job &b);是函数模板的一个显式实例化,只需声明,编译器遇到这种显式实例化,会根据原模板的定义及该声明直接生成一个实例函数,该函数仅接受job型。不然编译器遇到模板的使用时才会隐式的生成相应的实例函数。