1.1 泛型
函数模板是通用的函数描述,即它们使用**“泛型”**来定义函数。
➢其原理是将类型作为参数传递给模板, 使编译器生成该类型的函数。(注意,不是
源码编写阶段形成函数)
注意:函数模板的定义并不创建任何函数,只是告知编译器如何定义函数。
现编写一个泛型的交换函数模板。
#include <iostream>
using namespace std;
template <typename AnyType>
void Swap(AnyType &a, AnyType &b);
int main()
{
int i = 10;
int j = 23;
cout << "The original i is: " << i << " and j is: " << j << endl;
Swap(i,j);
cout << "Now the i is: " << i << " and j is: " << j << endl;
double a = 23.23;
double b = 32.32;
cout << "The original a is: " << a << " and b is: " << b << endl;
Swap(a,b);
cout << "Now the a is: " << a << " and b is: " << b << endl;
return 0;
}
template <typename AnyType>
void Swap(AnyType &a, AnyType &b)
{
AnyType temp;
temp = a;
a = b;
b = temp;
}
函数模板并没有缩短可执行程序,可执行程序仍然根据不同版本的生成函数而形成多个函数。
1.2 重载的模板
#include <iostream>
using namespace std;
template <typename AnyType>
void Swap(AnyType &a, AnyType &b);
template <typename AnyType>
void Swap(AnyType a[], AnyType b[],int n);
template <typename AnyType>
void showArray(AnyType arr1[],AnyType arr2[]);
const int SIZE = 8;
int main()
{
int i = 10;
int j = 23;
cout << "The original i is: " << i << " and j is: " << j << endl;
Swap(i,j);
cout << "Now the i is: " << i << " and j is: " << j << endl;
double a = 23.23;
double b = 32.32;
cout << "The original a is: " << a << " and b is: " << b << endl;
Swap(a,b);
cout << "Now the a is: " << a << " and b is: " << b << endl;
int arr1[SIZE] = {1,2,3,4,5,6,7,8};
int arr2[SIZE] = {8,7,6,5,4,3,2,1};
double arr3[SIZE] = {1.2,2.2,3.2,4.2,25.2,6.2,7.2,8.2};
double arr4[SIZE] = {8.1,7.1,6.1,5.1,4.1,3.1,2.1,1.1};
showArray(arr1,arr2);
Swap(arr1,arr2,SIZE);
showArray(arr1,arr2);
showArray(arr3,arr4);
Swap(arr3,arr4,SIZE);
showArray(arr3,arr4);
return 0;
}
template <typename AnyType>
void Swap(AnyType &a, AnyType &b)
{
AnyType temp;
temp = a;
a = b;
b = temp;
}
template <typename AnyType>
void Swap(AnyType a[], AnyType b[],int n)
{
AnyType temp;
for(int i = 0; i < n; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
template <typename AnyType>
void showArray(AnyType arr1[],AnyType arr2[])
{
cout << "The array1:" << endl;
for(int i = 0; i < SIZE; i++)
cout << arr1[i] << " ";
cout << endl;
cout << "The array2:" << endl;
for(int j = 0; j < SIZE; j++)
cout << arr2[j] << " ";
cout << endl;
}
输出为:
The original i is: 10 and j is: 23
Now the i is: 23 and j is: 10
The original a is: 23.23 and b is: 32.32
Now the a is: 32.32 and b is: 23.23
The array1:
1 2 3 4 5 6 7 8
The array2:
8 7 6 5 4 3 2 1
The array1:
8 7 6 5 4 3 2 1
The array2:
1 2 3 4 5 6 7 8
The array1:
1.2 2.2 3.2 4.2 25.2 6.2 7.2 8.2
The array2:
8.1 7.1 6.1 5.1 4.1 3.1 2.1 1.1
The array1:
8.1 7.1 6.1 5.1 4.1 3.1 2.1 1.1
The array2:
1.2 2.2 3.2 4.2 25.2 6.2 7.2 8.2
由程序可看出,它们是具有不同函数参数列表的函数模板。
1.3 模板的局限性
由于数据和操作往往是需要匹配的,因此:
- a = b;
如果是数组,则非法。 - if(a > b)
如果是结构体,则非法。
➢编写模板函数可能无法处理某些类型;
➢有时通用化是有意义的,但C++语法不允许直接这样操作,如两个坐标结构相加,但没有为
坐标定义“+”运算符。
◆ 解决方法一:为“+”定义重载操作符函数(类内容)。
◆ 解决方法二:为特定类型提供具体化的模板定义。即“显式具体化”
1.4 显式具体化
具体化方式:
#include <iostream>
const int SIZE = 10;
template <typename T>
void Swap(T &a,T &b);
struct job
{
char name[SIZE];
double salary;
int floor;
};
using namespace std;
template <> void Swap<job>(job &, job &);
int main()
{
job job1 = {"Amy",123.32,6};
job job2 = {"Bob",132.32,8};
cout << job1.name << "'s salary is: " << job1.salary << " on floor " << job1.floor << endl;
cout << job2.name << "'s salary is: " << job2.salary << " on floor " << job2.floor << endl;
Swap(job1,job2);
cout << job1.name << "'s salary is: " << job1.salary << " on floor " << job1.floor << endl;
cout << job2.name << "'s salary is: " << job2.salary << " on floor " << job2.floor << endl;
return 0;
}
template <> void Swap<job>(job &j1, job &j2)
{
double temp1;
int temp2;
temp1 = j1.salary;
j1.salary = j2.salary;
j2.salary = temp1;
temp2 = j1.floor;
j1.floor = j2.floor;
j2.floor = temp2;
}
Amy's salary is: 123.32 on floor 6
Bob's salary is: 132.32 on floor 8
Amy's salary is: 132.32 on floor 8
Bob's salary is: 123.32 on floor 6
提示:
就目前而言,函数有“非模板函数”、“模板函数”和“显式具体化模板函数”三种。
如果有多个原型,编译器在选择时,优先选择非模板,其次再选择显式具体化模板,最后再选择模板。
即: 非模板 > 显式具体化模板 > 模板
1.5 实例化和具体化
不是很懂,待补充。
资料参考 C++ Primer Plus (第六版)中文版,仅学习使用