可変引数テンプレートでの型リスト - reverse

型リストを逆順にするメタ関数です。

template <int N, class Seq>
struct reverse_impl;

template <int N, class Head, class... Tail>
struct reverse_impl<N, tuple<Head, Tail...>> {
    typedef
        typename concat<typename reverse_impl<N-1, tuple<Tail...>>::type,
                        tuple<Head>
                 >::type
    type;
};

template <class... Seq>
struct reverse_impl<0, tuple<Seq...>> {
    typedef tuple<Seq...> type;
};


template <class Seq>
struct reverse;

template <class... Seq>
struct reverse<tuple<Seq...>> {
    typedef typename reverse_impl<sizeof...(Seq), tuple<Seq...>>::type type;
};
reverse<tuple<int, double, long>>::type
→ tuple<long, double, int>