Рассмотрим следующую программу:
#include <iostream>
#include <algorithm>
using namespace std;
template<class T>
struct A {
typedef pair<T, T> PairType;
};
template<class T>
struct B {
void f(A<T>::PairType p) {
cout << "f(" << p.first << ", " << p.second << ")" << endl;
}
void g(pair<T, T> p) {
cout <<"g(" << p.first << ", " << p.second << ")" << endl;
}
};
int main() {
B<int> b;
b.f(make_pair(1, 2));
b.g(make_pair(1, 2));
}
Почему он не компилируется? Жалуется на деталь методом B::f()
. Кажется, он не распознает typedef в классе A<T>
. Если я изменю T
на конкретный тип, он все же работает. Полное сообщение об ошибке следующее:
g++ -DNDEBUG -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp:13: error: ‘template<class T> struct A’ used without template parameters
../main.cpp:13: error: expected ‘,’ or ‘...’ before ‘p’
../main.cpp: In member function ‘void B<T>::f(int)’:
../main.cpp:14: error: ‘p’ was not declared in this scope
../main.cpp: In function ‘int main()’:
../main.cpp:23: error: no matching function for call to ‘B<int>::f(std::pair<int, int>)’
../main.cpp:13: note: candidates are: void B<T>::f(int) [with T = int]
make: *** [main.o] Error 1
Я даже попробовал его по-другому, но он все равно не работал:
void f(A::PairType<T> p) {
cout << "f(" << p.first << ", " << p.second << ")" << endl;
}
Как можно заставить работать такой код?