我创建了两个简单的功能,其取得模板参数和一个空的结构限定类型:为什么模板参数演绎在这里不起作用?
//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 value
template <class A>
A
temp(typename S<A>::type a1)
{
return a1;
}
//Example 2: get two parameters by reference, perform the sum and return it
template <class A, class B>
B
temp2(typename S<A>::type a1, B a2)//typename struct S<B>::type a2)
{
return a1 + a2;
}
参数类型被施加到的struct来获取参考。我打电话给他们一些整数值,但编译器无法推断参数:
int main()
{
char c=6;
int d=7;
int res = temp(c);
int res2 = temp2(d,7);
}
错误1个错误C2783:“A 温度(S ::类型)”:无法推断 为模板参数'A'
错误2错误C2783: 'B TEMP2(S ::型,B)':可以为 'A' 不 演绎模板参数
这是怎么发生的?是否很难看到模板参数是char和int值?
欢迎来到SO。和+1 :) – 2009-08-12 21:44:17
我编辑了一段时间的报价。你首先,所以+1 :) – 2009-08-12 21:50:15