ad_m1n 2020-12-05 23:24 采纳率: 60%
浏览 2

为什么在下面的模板函数的形参列表中加上&就不能运行?是类型转换方面的原因吗?

#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;

template<class T>
T trans(T a,T b){
   return  a+b;
}
void test01(){
	int a=10;
	char c='a';
	cout<<trans<int>(a,c);

}
int main()
{
   test01();
}
#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;

template<class T>
T trans(T &a,T &b){
   return  a+b;
}
void test01(){
	int a=10;
	char c='a';
	cout<<trans<int>(a,c);

}
int main()
{
   test01();
}
  • 写回答

1条回答 默认 最新

  • 黄铎彦 2023-08-23 16:12
    关注

    是,这里不能把普通类型隐式转换为引用。

    评论 编辑记录

报告相同问题?