当函数模板和普通函数同名时,会发生什么,下面直接测试:
#include <iostream>
using namespace std;
template <typename T>
void test(T a, T b)
{
cout<<"函数模板:"<<a<<" "<<b<<endl;
}
void test(int a, int b)
{
cout<<"普通函数:"<<a<<" "<<b<<endl;
}
int main()
{
int a = 1;
char b = 'c';
test(b, a);
test(a, a);
test<>(a, a);
return 0;
}
输出结果:
普通函数:99 1
普通函数:1 1
函数模板:1 1
结果说明了
1. 普通函数的调用可以进行隐式的类型转换;函数模板的调用必须严格的按照类型匹配,不会进行类型转换。
2. 当普通函数和模板函数都满足条件时,会优先选择普通函数
3. 如果调用时添加了<>
类型列表,就会调用函数模板