samedi 24 février 2018

Folding a parameter pack of N types into N-1 pairs

I am trying to fold a parameter pack of N different types into a std::tuple of N-1 std::pairs with respective types.

So for example the expression

ResolveToTupleOfPairs<void, int, long>::Type tuple;

should evaluate to

std::tuple<std::pair<void, int>, std::pair<int, long>> tuple;

So I am searching for an implementation of the ResolveToTupleOfPairs type to fold the parameter pack as explained. My current implementation follows, but obviously it causes the type to be a tuple of pairs which each hold the same type twice instead of <T0, T1>, <T1, T2>, ....

template<typename... T>
struct ResolveToTupleOfPairs {
    static_assert(sizeof...(Args) > 1, "need at least two arguments");

    using Type = std::tuple<std::pair<T, T>...>;
};

I'm fine with c++17 solutions.

Aucun commentaire:

Enregistrer un commentaire