template<class A,class B>
void tmp(){
set<int,int>::iterator it; //works
set<A,B>::iterator it; // doesn't work
}
Run Code Online (Sandbox Code Playgroud)
由于C++语法中的一些相当恼人的限制,您必须set<A,B>::iterator使用typename关键字明确告诉C++ 是类型名称,而不是静态成员标识符.例如,这段代码编译得很好:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这是因为C++要求编译器set<A,B>::iterator在解析语法时最终决定是作为类型还是作为变量/函数进行解释; 在模板实例化之前.然而,为了模板实例之前,这是不可能作出这种确定,如在此可以取决于的值的一般情况A和B.因此,除非另有明确说明,否则编译器将其视为变量/函数.然后,这会导致解析错误.
| 归档时间: |
|
| 查看次数: |
1287 次 |
| 最近记录: |