当模板函数比普通函数更好匹配形参的时候,会优先调用模板函数
#include "iostream"
using namespace std;
template <class T>
void show(T a, T b)
{
cout << a << endl;
cout << b << endl;
cout << "temp show" << endl;
}
void show(int a, float b)
{
cout << a << endl;
cout << b << endl;
cout << "show" << endl;
}
int main()
{
int a = 100;
float b = 3.14;
int c = 99;
show(a, b);
show(a, c);
}
当模板函数和普通函数实现相同功能的时候,会优先调用普通函数
#include "iostream"
using namespace std;
template <class T>
void show(T a, T b)
{
cout << a << endl;
cout << b << endl;
cout << "temp show" << endl;
}
void show(int a, int b)
{
cout << a << endl;
cout << b << endl;
cout << "show" << endl;
}
int main()
{
int a = 100;
int c = 99;
show(a, c);
}
当模板函数和普通函数实现相同功能且不想调用普通函数时可用<>指定
#include "iostream"
using namespace std;
template <class T>
void show(T a, T b)
{
cout << a << endl;
cout << b << endl;
cout << "temp show" << endl;
}
void show(int a, int b)
{
cout << a << endl;
cout << b << endl;
cout << "show" << endl;
}
int main()
{
int a = 100;
int c = 99;
show<>(a, c);
}