2011-04-21 154 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.