I'm trying to implement a list using Template meta-programming but without success compiling the next code:
#include <iostream>
template<int Value>
struct Int {
static constexpr int value = Value;
};
template<typename H, typename ...TT>
struct List {
typedef H head;
typedef List<TT...> next; // <-- Too few template arguments for class template 'List'
constexpr static int size = sizeof...(TT) + 1;
};
int main() {
typedef List<Int<1>, Int<2>, Int<3>> list1;
static_assert(list1::head::value == 1, "Failed"); // = Int<1>
static_assert(list1::size == 3, "Failed"); // = 3
typedef typename list1::next list1Tail; // = List<Int<2>, Int<3>>
static_assert(list1Tail::head::value == 2, "Failed");
static_assert(list1Tail::size == 2, "Failed"); // = 2
typedef typename list1Tail::next list2Tail; // = List<Int<3>> <<---error: wrong number of template arguments (0, should be at least 1)
static_assert(list2Tail::head::value == 3, "Failed");
static_assert(list2Tail::size == 1, "Failed");
std::cout << "Passed" << std::endl;
}
With an error:
In instantiation of ‘struct List<Int<3> >’: error: wrong number of template arguments (0, should be at least 1)
I understand that in my case List must handle two types H and ...TT, but:
- Why calling
List<TT...>isn't enough? - How should I fix it?
- What is the best approach in this case for compiling the code?