0 c++ variadic-templates c++11
我正在尝试使用c ++ 11中的vadiadic模板定义一个IntList类,但我在语法上遇到了困难,我不知道如何初始化类字段.
我的最新版本如下:
template <int...>
struct IntList;
template<>
struct IntList<>{
constexpr static bool empty = true;
constexpr static int size = 0;
};
template<int Hd>
struct IntList<Hd>{
constexpr static bool empty = false;
constexpr static int size = 1;
constexpr static int head = Hd;
constexpr static IntList<> next = IntList<>();
};
template<int Hd, int... Rs>
struct IntList<Hd, Rs...>{
constexpr static bool empty = false;
constexpr static int size = sizeof ...(Rs);
constexpr static int head = Hd;
constexpr static IntList<Rs...> next = IntList<Rs...>();
};
Run Code Online (Sandbox Code Playgroud)
我的列表类有4个字段,head字段返回列表中的第一个数字,下一个字段返回列表的"tail".
对于包含2个或更多数字的列表以及包含1个数字的列表和空列表的2个基本情况,我有一个"一般"情况,它不包含head和next字段(空列表在尝试时会抛出错误访问其中之一).
在尝试测试我的列表时,该行:
IntList<1, 2, 3>::next::next;
Run Code Online (Sandbox Code Playgroud)
给我以下错误:
error: 'IntList<1, 2, 3>::next' is not a class, namespace, or enumeration
IntList<1, 2, 3>::next::next;
Run Code Online (Sandbox Code Playgroud)
尝试将头部和下一个字段定义为常规(非静态)字段并在构造函数内初始化它们也会导致错误:
invalid use of non-static data member 'IntList<1, 2, 3>::head'
IntList<1, 2, 3>::head;
Run Code Online (Sandbox Code Playgroud)
这让我相信我应该将这两个字段定义为"静态".
关于如何定义头部和下一个字段/我做错了什么的任何输入将不胜感激!