简体   繁体   English

什么是struct NIL {typedef NIL Head; }?

[英]What is struct NIL { typedef NIL Head; }?

I am reading about template metaprogramming. 我正在阅读模板元编程。 I could not understand what these lines mean; 我无法理解这些线是什么意思; the following code concerns about doing metaprogramming on a linked list. 以下代码涉及在链表上进行元编程。

struct NIL {
typedef NIL Head;
typedef NIL Tail;
};

template <typename H, typename T=NIL> struct Lst {
    typedef H Head;
    typedef T Tail;
};

template <int N> struct Int{ static const int result = N; };
typedef Lst< Int<1>, Lst< Int<2>, Lst< Int<3> > > > OneTwoThree;

The above is coming from https://monoinfinito.wordpress.com/series/introduction-to-c-template-metaprogramming/ . 以上内容来自https://monoinfinito.wordpress.com/series/introduction-to-c-template-metaprogramming/ I would greatly appreciate any guidance as to what it means to have struct NIL, which has typedef NIL Head, and typedef NIL Tail. 我非常感谢有关struct NIL具有什么意义的指导,它具有typedef NIL Head和typedef NIL Tail。 Nil is a type, sure, but if I have typedef NIL Head, for example, does it mean i have another recursive Head and Tail within each Head? Nil是一种类型,当然,但是如果我有typedef NIL Head,例如,这是否意味着我在每个头部内都有另一个递归的Head和Tail?

No, typedef NIL Head; 不, typedef NIL Head; doesn't mean you have a NIL within each NIL . 并不意味着你有一个 NIL每个内NIL

It simply means NIL::Head is another name for NIL . 它只是意味着NIL::HeadNIL另一个名称。 Ditto for NIL::Tail . 同样适用于NIL::Tail

This does mean you can write the types recursively, for example NIL::Head::Tail::Tail::Tail::Head::Head is also a name for NIL . 这意味着你可以递归地编写类型,例如NIL::Head::Tail::Tail::Tail::Head::Head也是NIL的名称。 However there's no actual recursive type here. 但是这里没有实际的递归类型。

NIL is just another name for nothing. NIL只是另一个名字。

It isn't NULL because that was taken. 它不是NULL因为这是采取的。

Here, we are creating a LISP like linked list of "head" and "tail". 在这里,我们正在创建一个LISP,如“head”和“tail”的链接列表。 To represent the end of the list, place a NIL . 要表示列表的末尾,请放置一个NIL

NIL satisfies the layout of a list, but one with both head and tail being NIL . NIL满足列表的布局,但是头部和尾部都是NIL Myself I'd be tempted to have written: 我自己会被写成:

struct NIL;

template <typename H, typename T=NIL> struct Lst {
  typedef H Head;
  typedef T Tail;
};

struct NIL:Lst{}; struct NIL:Lst {};

to make NIL as the name of the empty list more clear. 使NIL作为空列表的名称更加清晰。

By making NIL have head & tail, some template metaprogramming becomes easier (and some becomes harder). 通过使NIL具有头部和尾部,一些模板元编程变得更容易(并且一些变得更难)。 The fact that they are the same type does mean that in one sense the NIL list has an infinite length; 它们是相同类型的事实意味着在某种意义上, NIL列表具有无限长度; but in another sense it has zero length, as we define length to be the distance until we reach NIL . 但在另一种意义上它的长度为零,因为我们将长度定义为距离达到NIL之前的距离。

typedef s are not "within" the type, they are defined inside like how data members are. typedef不在类型内,它们在内部定义,就像数据成员一样。 They are more like pointers than members. 它们更像是指针而不是成员。 In non template metaprogramming: 在非模板元编程中:

struct node {
  node* head, *tail;
};
struct NIL:node{
  NIL() : node{this, this} {}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM