我试图将对类模板的(有限的)理解扩展到具有模板模板参数的类模板。

此声明和构造函数可以正常工作(很好,可以编译):

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

Difference between template-name and template-id.

关于c++ - 使用模板模板参数的模板化构造函数的正确语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5748218/

10-11 22:48
查看更多