函数模板
用 函数模板 解决
template<class 类型参数1, class 类型参数2, … >
返回值类型 模板名 (形参表)
{
函数体
用 函数模板 解决
template<class 类型参数1, class 类型参数2, … >
返回值类型 模板名 (形参表)
{
函数体
}
C++编译器遵循以下优先顺序:
Step 1: 先找参数完全匹配的普通函数(非由模板实例化
而得的函数)
Step 2: 再找参数完全匹配的模板函数
Step 3: 再找实参经过自动类型转换后能够匹配的普通
函数
Step 4: 上面的都找不到, 则报错
#include <iostream>
using namespace std;
template<class T1,class T2>
void print(T1 &a,T2 &b)
{
cout<<"using the print1"<<endl;
}
template<class T>
void print(T &a,T&b)
{
cout<<"using the print2"<<endl;
}
void print(int &a,int &b)
{
cout<<"using the print"<<endl;
}
int main()
{
int a=1,b=3;
print(a,b);
int c=1;double d=1.2;
print(c,d);
//system("pause");
return 0;
}