为什么模板参数推导在这里不起作用?
我创建了两个简单的函数,它们获取模板参数和一个定义类型的空结构:
//S<T>::type results in T&template <class T>struct S{
typedef typename T& type;};//Example 1: get one parameter by reference and return it by valuetemplate <class A>A
temp(typename S<A>::type a1){
return a1;}//Example 2: get two parameters by reference, perform the sum and return ittemplate <class A, class B>B
temp2(typename S<A>::type a1, B a2)//typename struct S<B>::type a2){
return a1 + a2;}参数类型应用于结构S以获得引用。我用一些整数值来调用它们,但是编译器无法推断出参数:
int main(){
char c=6;
int d=7;
int res = temp(c);
int res2 = temp2(d,7);}错误1错误C2783:“ A temp(S :: type)”:无法推断出“ A”的模板参数
错误2错误C2783:“ B temp2(S :: type,B)”:无法推断出“ A”的模板参数
为什么会这样呢?很难看出模板参数是char和int值吗?
www说
茅侃侃
白衣非少年
江户川乱折腾
随时随地看视频慕课网APP
相关分类