函数模板与模板显式具体化
#include "iostream"
using namespace std;
template <class anytype>
anytype add(anytype a,anytype b)
{
return a+b;
}
struct job
{
int a;
int b;
};
template <> job add(job a, job c)
{
job b;
b.a=a.a+c.a;
b.b=a.b+c.b;
return b;
}
int main(void)
{
int a=1,b=2;
cout<<add(a,b)<<endl;
job c={1,1};
c=add(c,c);
cout<<c.a+c.b<<endl;
}
模板做形参时,函数重载解析
#include "iostream"
using namespace std;
template <typename T> void add(T a,T b)
{
cout<<"claa fun 1"<<endl;
}
template <typename T> void add(T* a,T* b)
{
cout<<"claa fun 2"<<endl;
}
template <typename T> void add(T& a,T& b)
{
cout<<"claa fun 3"<<endl;
}
template <typename T> void add(const T& a,const T& b)
{
cout<<"claa fun 4"<<endl;
}
int main(void)
{
}