4
template<class A,class B>
void tmp(){
set<int,int>::iterator it; //works
set<A,B>::iterator it; // doesn't work
}
template<class A,class B>
void tmp(){
set<int,int>::iterator it; //works
set<A,B>::iterator it; // doesn't work
}
的由于C++的语法,你必须明确地告诉C++是set<A,B>::iterator
是一种名称,而不是静态成员标识符,使用typename
关键字一些相当烦人的限制。例如,该代码编译就好:
#include <set>
template<class A, class B>
void tmp() {
std::set<int,int>::iterator x; // OK
typename std::set<A,B>::iterator it; // Also OK
}
int main() {
tmp<int,int>();
return 0;
}
这是因为C++要求编译器做出最终决定是否解释set<A,B>::iterator
作为一种类型或当它的解析语法的可变/功能; 之前该模板被实例化。但是,在模板实例化之前,不可能做出该确定,因为在一般情况下,这可能取决于A
和B
的值。因此,除非另有明确说明,否则编译器将假定它是一个变量/函数。这会导致解析错误。 “
”即使在set :: ite之前使用typename ...我无法制作基于模板的容器的迭代器。“... – easyonesorry 2011-02-07 17:13:18