我试图将我对类模板的(有限)理解扩展到具有模板模板参数的类模板。
这个声明和构造函数工作正常(嗯,它可以编译):
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 g++。上面那行有什么问题吗?
最佳答案
试试这个
template< template<char v> class Char2Type >
Test2<Char2Type>::Test2( void )
{
}
模板模板参数的模板参数应是类模板的名称。
Char2Type
是模板名称,而 Char2Type<char>
是模板 ID。您不能使用template-id
代替template-name
在你的例子中。
关于c++ - 使用模板模板参数的模板化构造函数的正确语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5748218/