【问题标题】:Proper syntax for a templated constructor using template template parameters使用模板模板参数的模板化构造函数的正确语法
【发布时间】:2011-04-21 18:25:09
【问题描述】:

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

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

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 )
{
} 

但是,当我尝试使用模板化模板参数做类似的事情时,我得到了这些错误(导致错误的行如下):

错误:缺少“>”以终止模板参数列表 错误:模板参数 1 无效 错误:缺少“>”以终止模板参数列表 错误:模板参数 1 无效 错误:“>”标记之前的预期初始化程序
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++。上面这行有什么问题??

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    试试这个

    template< template<char v> class Char2Type >
    Test2<Char2Type>::Test2( void ) 
    {
    }
    

    模板模板参数的模板参数应该是类模板的名称Char2Type 是模板名称,而 Char2Type&lt;char&gt; 是模板 ID。在您的示例中,您不得使用 template-id 代替 template-name

    Difference between template-name and template-id.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 2011-05-08
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多