gpt4 book ai didi

c++ - 元编程,模板参数数量错误

转载 作者:行者123 更新时间:2023-11-28 05:16:32 25 4
gpt4 key购买 nike

我正在尝试 C++ 元编程。我有一个 TypeList 类,它包含一个头部类型和一个尾部类型,它是另一个 TypeList 或列表末尾的 NullType。

我还有一个 IntegerList 类,它是整数指针列表(好吧,它是一个日志系统,但我删除了尽可能多的代码,并且 Integer* 元素替换了 Logger* 元素)

我的文件无法编译,因为 g++ 提示我使用了“错误数量的模板参数(1,应该是 2)”,我不明白为什么。

我哪里做错了?我只是从我信任的网站上复制了一些代码,它应该可以工作......我主要决定使用元编程,因为它似乎是一件非常有趣的事情,但我已经开始失去理智了 ^^。

提前谢谢你。

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

PS2。我认为这个冷代码不会编译,因为 Integer 不是一个类,但我在实际类中遇到了同样的错误,所以我相信我的错误发生在检查之前。

整数列表.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;
}
};

主要.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。以下编译:

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

关于c++ - 元编程,模板参数数量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537842/

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