下面我给出两个程序及其输出。
代码1:
#include<iostream>
using namespace std;
template <class X,class Y> X sumargs(X a,Y b)
{
cout<<"\nThe sum is :" << a+b;
}
int sumargs(int a,char b)
{
cout<<"\nThis works\n";
return 1;
}
int main()
{
sumargs<int>(1,2);
sumargs<char>(4,9.0);
sumargs<double>('d',8);
sumargs(7,'a');
return 0;
}
输出 1:
The sum is :3
The sum is :13
The sum is :108
This works
代码2:
#include<iostream>
using namespace std;
template <class X,class Y> X sumargs(X a,Y b)
{
cout<<"\nThe sum is :" << a+b;
}
int sumargs(int a,char b)
{
cout<<"\nThis works\n";
return 1;
}
int main()
{
sumargs<int>(1,2);
sumargs<char>(4,9.0);
sumargs<double>('d',8);
sumargs<int>(7,'a');
return 0;
}
输出 2:
The sum is :3
The sum is :13
The sum is :108
The sum is :104
为什么是sumargs(7,'a'); 在代码 2中没有调用函数的显式重载版本?