【发布时间】:2014-03-31 00:06:57
【问题描述】:
为什么以下内容符合要求有点困惑:
template<typename T>
struct Foo {
template<int i>
struct Bar {
typedef int BarSizeType;
};
};
template<typename T, int i>
void do_something(typename Foo<T>::Bar<i>::BarSizeType arg) {
// ...
}
但这不是:
template<typename T, typename T2>
struct Foo {
template<int i>
struct Bar {
typedef int BarSizeType;
};
};
template<typename T, int i>
void do_something(typename Foo<T, T>::Bar<i>::BarSizeType arg) {
// ...
}
编译错误是:
错误 C2143:语法错误:在 ' 之前缺少 ')' 错误 C2143:语法错误:缺少“;”在'之前 错误 C2988:无法识别的模板声明/定义
错误 C2059:语法错误:' 错误 C2039:“BarSizeType”:不是“全局命名空间”的成员
错误 C2059:语法错误:')'
错误 C2143:语法错误:缺少“;”在'{'之前
错误 C2447:“{”:缺少函数头(旧式正式列表?)
有什么方法可以编译,而无需对代码进行重大更改?我正在使用 vs2012 编译器。
【问题讨论】: