Stworzyłem dwie proste funkcje, które ulegną parametry szablonu i pusty struct określającą typ:Dlaczego odliczanie argumentu szablonu nie działa tutaj?
//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;
}
Argument type jest zastosowane do struct S uzyskać odniesienie. Nazywam je z pewnymi wartościami całkowitymi ale kompilator nie jest w stanie wydedukować argumenty:
int main()
{
char c=6;
int d=7;
int res = temp(c);
int res2 = temp2(d,7);
}
Error 1 error C2783: 'a temp (S :: type)': nie można wywnioskować argument szablonu 'A'
błąd 2 error C2783: 'B temp2 (S :: typ B)': nie mógł wywieść szablon argumentem dla 'A'
Dlaczego tak się dzieje? Czy trudno jest zauważyć, że argumenty szablonu są wartościami 01?
witamy w SO. i +1 :) –
Edytowałem cytat przez jakiś czas. Po pierwsze, więc +1 :) –