Den*_*nis 3 c++ templates nested stdmap
下面的代码在声明迭代器的行生成语法错误:
template <typename T>
class A
{
public:
struct B
{
int x, y, z;
};
void a()
{
std::map<int, B>::const_iterator itr; // error: ; expected before itr
}
std::vector<T> v;
std::map<int, B> m;
};
Run Code Online (Sandbox Code Playgroud)
这只有在A是模板化类时才会发生.这段代码出了什么问题?如果我将B移出A,代码编译得很好.
小智 8
你需要一个类型名称:
typename std::map<int, B>::const_iterator itr;
Run Code Online (Sandbox Code Playgroud)
迭代器是一个依赖类型(取决于B),当你遇到这种情况时,编译器要求你用一个typename来澄清它.
有问题的合理的讨论在这里.