|
 
#include <iostream>
using namespace std;

template <typename T1 , typename T2>
 T2 fun(T1 x,T2 y) {
cout<<"调用了两个个参数的 fun 函数"<<endl;
T2 tmp = x *x + y * y + x * y;
return tmp;
}

template <typename T>
 T fun(T x , T y , T z) { //模版函数的重载
cout<<"调用了三个参数的 fun 函数"<<endl;
T tmp = x * x + y * y + z * z + x * y * z;
return tmp;
}

 int main() {
int x1 = 1 , y1 = 4 , z1 = 5;
float x2 = 1.1 , y2 = 2.2;
double x3 = 2.0 , y3 = 3.1;
cout << fun(x1,y1) << endl;
cout << fun(x2,y2) << endl;
cout << fun(x3,y3) << endl;
cout << fun(x1,y2) << endl;
cout << fun(x1,y1,z1) << endl;
return 0;
}
//****************************************
#include <iostream>

using namespace std;

template <typename T>
 T fun(T x,T y) {
cout << "调用了模板函数 ^^ " << endl;
T tmp = x * x + y * y + x * y;
return tmp;
}
 int fun(int x,int y) {
cout << "调用了非模板函数"<<endl;
int tmp = x * x + y * y + x * y;
return tmp;
}

 int main() {
int x1 = 1 , y1 = 4;
float x2 = 1.1 , y2 = 2.2;
cout << fun(x1,y1) << endl;
cout << fun(x2,y2) << endl;
return 0;
}
 /**//******************************

看以看出模版函数和非模板函数也是可能重载的,
那么重载函数的调用顺序是怎么样的呢?实际上是先查找非模板函数,
要有严格匹配的非模板函数,就调用非模板函数,
找不到适合的非模板函数在和模板函数进行匹配。

*******************************/



|