APPENDING TO TYPELIST
Modifying a typelist is not possible, so we can create a new typelist and return by value which contains the output.
ALGORITHM:
Inputs: Typelist TList, type class T
Outputs: Inner type definition Result
If both TList and T are NullType, then the result is NullType.
Else
If TList is NullType and T is a single (non-typlist) type, then Result is a typelist having T as its only element.
Else
If TList is NullType and T is typelist, Result is T.
Else If TList is not-null, then Result is a typelist having TList::Head as Head and result of appending T to TList::Tail as its tail.
CODE:
template <class TList, class T> struct Append;
template <> struct Append<NullType, NullType>
{
typedef NullType Result;
};
template <class T> struct Append<NullType, T>
{
typedef TYPELIST_1(T) Result;
};
template <class Head, class Tail>
struct Append<NullType, Typelist<Head, Tail> >
{
typedef Typelist<Head, Tail> Result;
};
template <class Head, class Tail, class T>
struct Append<Typelist<Head, Tail>, T>
{
typedef Typelist<Head,
typename Append<Tail, T>::Result>
Result;
};
EXPLANATION
Partial specialization of Append recursively represents the append template.
There is a mixed operation for single types and typelists.
The list of all signed numeric data types in C++ is:
typedef Append<SignedIntegrals, TYPELIST_3(float, double, long double)>::Result SignedTypes;
Comments