因此,结果将类型作为参数a和b的同一个变量,当函数返回的类型。
在这种特定的情况下,通用型T作为总和的一个参数,编译器甚至能够推导出数据类型自动无需显式地指定在尖括号。因此,而不是显式指定模板参数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// function templates
#include <iostream>
using namespace std;
template <class T, class U>
bool are_equal (T a, U b)
{
return (a==b);
}
int main ()
{
if (are_equal(10,10.0))
cout << "x and y are equal\n";
else
cout << "x and y are not equal\n";
return 0;
}
因为,在C + +,没有后缀的整数文字量(如10)都是int类型,和浮点字面量没有后缀(如10)都是double类型,有没有歧义的可能,因此模板参数可以在呼叫略。