元编程,错误数量的模板参数

Metaprogramming, wrong number of template arguments

本文关键字:参数 编程 错误      更新时间:2023-10-16

我正在尝试C 元编程。我有一个打字机类,其中包含头部类型和尾巴类型,该类型是列表末尾的另一种打字机或nulltype。

我也有一个IntegerList类,它是整数上的指针列表(嗯,这是一个记录系统,但是我尽可能地删除了代码,并且Integer* elements替换了Logger* Elements)

我的文件不会编译,因为G 抱怨我使用的是"错误的模板参数(1,应为2)",而我不明白为什么。

我在哪里做错了?我只是从我信任的网站上复制了一些代码,它应该起作用...我主要决定使用元编程,因为这似乎是一件非常相关的事情,但是我已经开始失去我的思想^^。<<<<

预先感谢您。

ps。我有一个我无法更改的非常旧的G 编译器(2006),但是2013年的另一个G 给了我同样的错误。

PS2。我认为这种感冒不会汇编,因为整数不是一类,但是我在真实的课程中遇到了同样的错误,所以我相信我的错误在检查之前就会发生。

integerlist.hpp:

#include <list>
typedef int Integer;
class NullType
{
};
template<class H, class T>
class TypeList
{
    typedef H Head;
    typedef T Tail;
};
template<class T>
struct IntegerList : public std::list<Integer*>
{
};
template<>
struct IntegerList<NullType> : public std::list<Integer*>
{
};
template <class H, class T>
struct IntegerList<TypeList<typename H, typename T> > :  public std::list<Integer*>
{ //                                              ^ error right there
  typedef TypeList<typename H, typename T> List_t;
  typedef typename H Head_t;
  typedef typename T Tail_t;
  IntegerList()
  {
    push_back( new Head_t );
    IntegerList<Tail_t> tmp;
    merge( tmp );
  }
  ~IntegerList()
  {
    IntegerList<List_t>::iterator it;
    for ( it=begin(); it!=end(); ++it )
      delete *it;
  }
};

main.cpp:

#include "IntegerList.hpp"
int main(int argc, char **argv)
{  
   // wrong number of template arguments (1, should be 2)
  IntegerList<TypeList<Integer, NullType> > mylist;
}

问题似乎是您在IntegerListTypeList的使用情况下撒了几个typename s。以下编译:

template <class H, class T>
struct IntegerList<TypeList< H,  T> > :  public std::list<Integer*>
{ 
  typedef TypeList< H,  T> List_t;
  typedef H Head_t;
  typedef T Tail_t;
  . . .
}

完整示例http://coliru.stacked-crooked.com/a/5086e3015dc12ea0