本质上模板函数是可以发生重载的,他会调用最匹配的模板函数
#include<iostream>
#include<cstdio>
using namespace std;
template<int v,typename type_t>
struct A{
public:
union storage_t
{
type_t values[ v];
};
storage_t x;
template< typename T,typename U>
void f(T a,U b, storage_t& sto){
printf(" typename T,typename U 普通模板函数\n");
}
template< typename T,typename U>
void f(int a,U b, storage_t& sto){
printf(" int typename U 在参数列表中指定其中的参数类型\n");
}
template< typename T = float ,typename U>
void f(float a,U b, storage_t& sto){
printf("float typename U 在模板信息和参数列表中都加入参数类型\n");
}
template< typename U>
void f(char a,U b, storage_t& sto){
printf("char typename U 删去模板声明并在参数列表中都加入参数类型\n");
}
};
int main()
{
A<2,int> a = A<2,int>();
a.f((int)1 ,2,a.x);
a.f((float)1.1 ,2,a.x);
a.f('a' ,2,a.x);
return 0;
}