getsoubl
getsoubl

Reputation: 1055

Finding the best match using metaprogramming technique

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

Upvotes: 1

Views: 70

Answers (2)

max66
max66

Reputation: 66230

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.

Look at the definition of Rest

using Rest = typename LargestTypeT<PopFront<List>>::Type;

Rest is the largest type in List, excluding (PopFront) the first type (First).

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

Not exactly.

In first loop, First is int, Rest is bool.

In second loop (where Rest for first loop is selected as bool) First is bool and Rest is char.

In third loop (where Rest for second loop is selected as char) First is char and Rest is char.

In fourth loop you have the ground case (or basic case, as you prefer)

template<>
class LargestTypeT<Typelist<>>
{
 public:
  using Type = char;
};

that select the Type (the Rest in third loop) as char

Upvotes: 2

Cubic
Cubic

Reputation: 15683

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;

Upvotes: 2

Related Questions