2014-02-23 93 views
1

我刚刚更新到4.8.2 GCC(4.7),我现在得到了下面的代码警告:如何定义模板的模板化子类?

template <class T_base> 
class factory { 
private: 
    template <class T> 
    struct allocator : factory { 
        //^warning: invalid use of incomplete type 'class factory<T_base>' 
    }; 
}; 

为了避免该警告,我试图定义factorystruct allocator之外,但现在得到出现以下错误:

template <class T_base> 
class factory { 
private: 
    template <class T> 
    struct allocator; 
}; 

template <class T_base, class T> 
struct factory<T_base>::allocator<T> : factory<T_base> { 
        //^error: too few template-parameter-lists 
}; 

我在做什么错?是否有上述构造的语法避免了警告和错误?

回答

3

你需要拼一下这样的:

template <class T_base> 
template <class T> 
struct factory<T_base>::allocator : factory<T_base> 
{ 
    // ... 
}; 
+0

现在有道理我知道需要什么:)谢谢! – zennehoy

+0

在这个上下文中'factory'不是_nested-type-name_(或其他)>有点烦人。< –

1

宣告嵌套模板正确的语法是有两个单独的模板参数列表:

template <class T_base> 
template <class T> 
struct factory<T_base>::allocator : factory<T_base> { 
}; 

不过,我质疑其语义意义上,这一段代码使。

+0

@KerrekSB没关系,这是一个复制和粘贴错误。我纠正了它。 –