mercredi 29 novembre 2017

Operations over list of ints with template metaprogramming [C++11]

I have implementation of list of ints with template:

template<int ... Int>
struct IntList;

template<int H, int ... T>
struct IntList<H, T...>{
    static const int Head = H;
    using Tail = IntList<T...>;
};
template<>
struct IntList<>{};

I want to define metafunctions to work with IntList: IntCons that allows to increase list by one element, and Generate that allows to generate a list of length N with ints from 0 to N-1 (Example of usage: using L = Generate<5>::type; // IntList<0,1,2,3,4>). I define IntCons this way:

template<int H, typename IL>
struct IntCons;

template<int H, int... Tail>
struct IntCons<H, IntList<Tail...>>{
    using type = IntList<H, Tail...>;
};

And i can't define metafunction Generate in such way that use function IntCons inside. Here's hint, according to i need use default parameter in Generate.

template<int N /*, typename IL =  default parameter?*/ >
struct Generate;

template<int N /*, typename IL =  default parameter?*/ >
struct Generate<N>{
    using type = ......;
};

What are the ways to define the meta-function Generate, how can i implement it?

Aucun commentaire:

Enregistrer un commentaire