Converting Typelist to Parameterpack - GerdHirsch/GenericTools GitHub Wiki
With
Loki::Typelist
there are many tools available to manipulate lists of types i.e. Reverse<..>
.
To be able to use these tools for parameter packs, the packs should be convertible from and to
Loki::Typelist
. This is done by
TList2TPack<typelist>
:= Typelist to Typepack.
using typelistABC = MakeTypelist<A, B, C>;
using packABC = TList2TPack<typelistABC>; // Typelist to Typepack
With this in place, all type-functions of Loki for Typelist can be used:
using packCBA = Reverse<packABC>;
using packCBA_2 = Reverse<A, B, C>;
The implementation of the type-function Reverse<..>
is:
template<class ...Pack>
struct ReverseImpl{
using typelist = MakeTypelist<Pack...>;
using reverseList = typename Loki::TL::Reverse<typelist>::Result;
using type = TList2TPack<reverseList>;
};
template<class ...Pack>
struct ReverseImpl<Typepack<Pack...>> : ReverseImpl<Pack...>{};
// Reverse<..> is the interface to the type-function
template<class ...pack>
using Reverse = typename ReverseImpl<pack...>::type;`
Note: Reverse<..>
is not specialized for Typepack<..>
but can be used with!
The outcome of the call Reverse<packABC>
above is a parameter pack with one argument: packABC
of type Typepack<A, B, C>
and this is delegated to ReverseImpl<..>
which is specialized for that kind of type.
Because ReverseImpl<..>
is a type-function, the created type is always a Typepack<..>
nevertheless the argument was a parameter ...pack
or a Typepack<..>
.
In Demo there are complete examples.