Dlaczego dedukcja szablonów nie działa tutaj?
Stworzyłem dwie proste funkcje, które pobierają parametry szablonu i pustą strukturę definiującą typ:
//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;
}
Typ argumentu jest stosowany do struktury S, aby uzyskać odniesienie. Nazywam je pewnymi wartościami całkowitymi, ale kompilator nie jest w stanie wywnioskować argumentów:
int main()
{
char c=6;
int d=7;
int res = temp(c);
int res2 = temp2(d,7);
}
Błąd 1 błędu C2783: „A temp (S :: type)”: nie można wywnioskować argumentu szablonu dla „A”
Błąd 2 błędu C2783: „B temp2 (S :: typ, B)”: nie można wywnioskować argumentu szablonu dla „A”
Dlaczego to się dzieje? Czy trudno jest dostrzec, że argumenty szablonu sązwęglać iint wartości?