c++函数模板中函数类型和形参类型所用的虚拟类型能不能不一样?比如: template<typen

2025-05-12 21:31:47
推荐回答(2个)
回答(1):

由于返回值类型没有办法直接推导出来,所以需要显式指定出来,当然也有其他方法,我就不太清楚了,看这个例子:
#include

using namespace std;

template
T1 Max(T2 a,T3 b)
{
T1 temp;
if(a>b)
temp=a;
else
temp=b;
return static_cast(temp);//显式指定类型
}

int main()
{//int Max(short a,int b);
short a=1;
int b=2;
cout<<"max:"<(a,b)<
return 0;

}

希望能帮到你

回答(2):

可以的,就是你传入的类型