在下面的程序,为什么编译器生成的调用printMax
模板函数,而不是调用printMaxInts
功能的错误?为什么编译器不能将此调用解析为模板函数?
#include <iostream>
template<class A>
void printMax(A a,A b)
{
A c = a>b?a:b;
std::cout<<c;
}
void printMaxInts(int a ,int b)
{
int c = a>b?a:b;
std::cout<<c;
}
int main()
{
printMax(1,14.45);
printMaxInts(1,24);
}
另一个解决方法是取两个模板参数,在函数中执行转换。 – Potatoswatter 2010-08-28 19:00:01