I am investigating the best match algorithm whitch is represented in Template, the Complete Guide The implementation is the following
template<typename List>
class FrontT;
template<typename Head, typename... Tail>
class FrontT<Typelist<Head, Tail...>>
{
public:
using Type = Head;
};
template<typename List>
using Front = typename FrontT<List>::Type;
template<typename List>
class PopFrontT;
template<typename Head, typename... Tail>
class PopFrontT<Typelist<Head, Tail...>> {
public:
using Type = Typelist<Tail...>;
};
template<typename List>
using PopFront = typename PopFrontT<List>::Type;
template<typename List>
class LargestTypeT;
// recursive case:
template<typename List>
class LargestTypeT
{
private:
using First = Front<List>;
using Rest = typename LargestTypeT<PopFront<List>>::Type;
public:
using Type = IfThenElse<(sizeof(First) >= sizeof(Rest)), First, Rest>;
};
// basis case:
template<>
class LargestTypeT<Typelist<>>
{
public:
using Type = char;
};
template<typename List>
using LargestType = typename LargestTypeT<List>::Type;
That I hard understand is the following line
using Type = IfThenElse<(sizeof(First) >= sizeof(Rest)), First, Rest>;
It is clear that the First
is the First Element in TypeList
and sizeof
it is the size of this type. But I confused with the Rest
. What is the size of Rest
? The Rest
it is a list with the rest of the elements that are included in typelist.
For example, if the following is defined
LargestType<TypeList<int,bool,char>>
In the first loop
First
is int
and sizeof
is 4
Rest
is bool,char
, what is the sizeof
?
In the second loop
First
is bool
and sizeof
is 1
Rest
is char
and sizeof
is 1
What is the size of Rest? The Rest it is a list with the rest of the elements that are included in typelist.
No, Rest
(which is a bit poorly named) is actually not the rest of the list, but rather the largest type in the rest of the list, as you can see from its definition:
using Rest = typename LargestTypeT<PopFront<List>>::Type;