我有以下代码:
template <typename Provider>
inline void use()
{
typedef Provider::Data<int> D;
}
我基本上是在尝试使用某个“Provider”类的模板类成员“Data”,应用于“int”,但出现以下错误:
util.cpp:5: error: expected init-declarator before '<' token
util.cpp:5: error: expected `,' or `;' before '<' token
我在 Solaris 系统上使用 GCC 4.3.3。
最佳答案
typedef typename Provider::template Data<int> D;
问题是,当编译器解析 use()
第一次,它不知道Provider
,所以它不知道什么 Provider::Data
指的是。它可以是静态数据成员、成员函数的名称或其他内容。这就是为什么你必须把 typename
英寸
额外的 template
只要嵌套名称是模板的名称,它就是必需的。如果是别的东西,那么Data < ...
可以做个比较。
关于c++ - 无法引用嵌套在模板参数中的模板名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2860390/