c++ - 使用模板模板参数的模板化构造函数的正确语法

标签 c++ templates

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

这个声明和构造函数工作正常(嗯,它可以编译):

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/

相关文章:

C++ - 使用父类属性

c++ - 在运行时按索引访问 std::tuple 元素的最佳方式

c++ - 读取字符串时发生核心转储?

c++ - 派生模板类访问基类成员数据

C++ 十六进制数字格式

c++ - 具有成员函数模板的类模板,其模板参数不是函数参数

c++ - 自动用指向部分专用函数成员的指针填充 vector

html - 如何在 CKEditor 中创建自定义 html 模板?

c++ - 对大字符串进行更快的操作

c++ - 是否有可能使 `=` 比(删除的)复制分配更喜欢从转换分配?