2014-02-23 44 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沒關係,這是一個複製和粘貼錯誤。我糾正了它。 –