1、函数模板
#include<iostream>
using namespace std;
template <class YY ,typename T>//模板
void fun(YY b,T a)
{
cout << b << " " << a << endl;
}
//template <typename T>//模板
//void fun(T a)
//{
// cout << a << endl;
//}
//void fun(double b)
//{
// count << b << endl;
//}
int main()
{
fun('a', 12);
system("pause");
return 0;
}
2、 函数模板及其具体化( 将指定的类型,单独处理)
#include<iostream>
using namespace std;
struct Node
{
int a;
double b;
};
template <typename T>//模板
void fun(T a)
{
cout << a << endl;
}
template <>void fun<Node>(Node no)
{
cout << no.a << " " << no.b << endl;
}
int main()
{
Node no = { 12,12.123 };
fun(no);
system("pause");
return 0;
}
3、实例化(生成指定类型的函数定义)
#include<iostream>
using namespace std;
struct Node
{
int a;
double b;
};
template <typename T>//模板
void fun(T a)
{
cout << a << endl;
}
template <>void fun<int>(int a);
int main()
{
/*Node no = { 12,12.123 };
fun(no);*/
system("pause");
return 0;
}
*盗用调用顺序:原版函数>具体化 >模板。