#include <iostream>
#include <string>
using namespace std;
/*
调用规则:
1.如果函数模板和普通函数都可以实现,优先调用普通函数
2.可以通过空模板参数列表来强制调用函数模板
3.函数模板也是可以发生重载
4.如果函数模板可以产生更好的匹配,优先调用函数模板
*/
void Function(int a, int b)
{
cout << "This is ordinary function" << endl;
}
template <class T>
void Function(T a, T b)
{
cout << "Call the template function!" << endl;
}
// 函数模板重载
template <class T>
void Function(T a, T b, T c)
{
cout << "Call the template22222 function!" << endl;
}
void test1()
{
int a = 1, b = 2;
char c = 'a', d = 'd';
// Function(a, b);
// 通过空模板参数列表,强制调用函数模板
// Function<>(a, b);
// 函数重载
// Function(a, b, 20);
// 如果遇到更好的匹配,优先调用函数模板
Function(d, c);
}
int main()
{
test1();
return 0;
}
C++ 函数模板调用规则
最新推荐文章于 2024-05-16 14:05:08 发布