使用元编程技术寻找最佳匹配

Finding the best match using metaprogramming technique

我正在研究完整指南模板中表示的最佳匹配算法 实现如下

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;

我很难理解的是下面这行

using Type = IfThenElse<(sizeof(First) >= sizeof(Rest)), First, Rest>;

很明显FirstTypeList中的第一个元素,sizeof是这个类型的大小。但是我混淆了RestRest 的大小是多少? Rest 它是一个列表,其中包含 typelist 中包含的其余元素。

例如,如果定义如下

LargestType<TypeList<int,bool,char>>

在第一个循环中 Firstintsizeof 是 4 Restbool,char ,什么是 sizeof ? 在第二个循环 Firstboolsizeof 是 1 Restcharsizeof 是 1

What is the size of Rest? The Rest it is a list with the rest of the elements that are included in typelist.

不,Rest(名字有点不好)实际上不是列表的其余部分,而是列表其余部分中最大的类型,正如您从其定义中看到的那样:

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

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.

Rest

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

RestList中最大的类型,不包括(PopFront)第一个类型(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

不完全是。

在第一个循环中,FirstintRestbool

在第二个循环中(其中第一个循环的 Rest 被 select 编辑为 boolFirstboolRestchar.

在第三个循环中(第二个循环的 Rest 被 select 编辑为 charFirstcharRestchar.

在第四个循环中,你有基本情况(或基本情况,如你所愿)

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

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