gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:19:46 25 4
gpt4 key购买 nike

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

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

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 listerror: template argument 1 is invaliderror: missing ‘>’ to terminate the template argument listerror: template argument 1 is invaliderror: 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com