2011-04-21 142 views
1

我試圖將我的(有限)對類模板的理解擴展爲具有模板模板參數的類模板。使用模板模板參數的模板構造函數的正確語法

該聲明和構造函數工作正常(當然,它編譯):

template < char PROTO > 
class Test 
{ 
public: 
    Test(void); 
    ~Test(void); 
    void doIt(unsigned char* lhs, unsigned char* rhs); 
}; 


template< char PROTO > 
Test<PROTO>::Test(void) 
{ 
} 

但是,當我嘗試使用模板模板參數做同樣的事情,我得到這些錯誤(來源誤差線下方):

 
error: missing ‘>’ to terminate the template argument list 
error: template argument 1 is invalid 
error: missing ‘>’ to terminate the template argument list 
error: template argument 1 is invalid 
error: expected initializer before ‘>’ token 
template <char v> struct Char2Type { 
enum { value = v }; 
}; 


template < template<char v> class Char2Type > 
class Test2 
{ 
public: 
    Test2(void); 
    ~Test2(void); 
    void doIt(unsigned char* lhs, unsigned char* rhs); 
}; 


template< template<char v> class Char2Type > 
Test2< Char2Type<char v> >::Test2(void) //ERROR ON THIS LINE 
{ 
} 

我使用GNU克++。上面的行有什麼問題?

回答

3

試試這個

template< template<char v> class Char2Type > 
Test2<Char2Type>::Test2(void) 
{ 
} 

一個模板參數模板的模板參數應爲名類模板的Char2Type是模板名稱,而Char2Type<char>是模板標識。在您的示例中,您不能使用template-id代替template-name

Difference between template-name and template-id.