C ++ Metaprogramming :: Typelist :: Definitions and Basic Implementations

xiaoxiao2021-03-06  65

Version 1.00

Author: prototype

Copyright (C) 2004 prototypePermission is granted to copy and redistribute this article provided that its contents (including the title, author information, and this copyright claim) are kept intact in the original form.This software is distributed in the hope that it will be Useful, But without Any Warranty; WITHOUT Even The Implied Warranty of Merchantability Or Fitness for a particular purpose.

AbstractThe typelist and relevant concepts are introduced, together with a basic typelist implementation. An elegant, easy-to-use, and macro-free typelist generator is shown.Introduction Typelist, as its name suggests, refers to a (singly-) linked- list "data" structure with the data or elements being types Like the conventional and more familiar singly-linked list data structure, typelist consists of a number of nodes that are arranged in a logically-linear fashion;. specifically, each node conprises two fields : One, Referred To As The "Data Field", Holds The Data; The Other One, Referred To As The "Pointer Field", Stores the Information for Finding The next node. The Pointer Field of The Last Node Normally has a singular "value" indicating the end of the list. typelist is as fundamental and important for C template metaprograming, which is becoming a crucial paradigm in moden C design and programming, as plain array for conventional programming. Examples of applications o f typelist can be found in Andrei Alexandrescu's book: Modern C Design, and also in future installments of this C Metaprogramming series Here I focus on a basic implementation of typelist and a solution to the interesting question:. how to generate a typelist without C- ? type macros A basic implementation of typelist Implementation of typelist can be amazingly simple as long as one knows how to store a data and how to represent a pointer in C template metaprogramming The solutions to the two problems turn out to be the same. - Use typedf. let us Take a look at an example to see how it works. typedef int type; // stores `int 'to` type'

.Type i; // `I 'is of type` int'. this Simple Example Illustrate That After Typedef, `Type 'Is Remembed by The Compiler to Stand for` Int' and thereafter Wherever `Type 'is buy, IT Will work as if it was literally replaced by `int 'This effect is exactly what we want - using a symbol to represent a data (a type) With this understood, storing a data in C template programming is straightforward;.. while the pointer used in typelist can be considered as an alias to the next node, therefore it can also be implemented using a typedef A basic implementation of typelist can be something like template struct typelist {typedef T type;. typedef SL sublist;}; Obviously, with this implementation, we want to use `type 'to store the data in this node and` sublist' to store the pointer to the next node to construct a typelist would be something like typedef typelist > TL2; `TL2 'Here Is A Typelist with Two Nodes. in the First Node, We Store `Int '; in the second node,` BOOL'. The `Sublist 'of the first node is` Typelist ' - the second node; but what about the `Sublist 'of the SECOND NODE? As I Mentioned Above We NEED A "Singular" Value for the Pointer of the Last Node. So the `? 'Should Be Replace by a Type That IS NOSELIST. THIS TYPEEPELIST. THIS TYPE IS Easy To Obtain, for Example: Class Null_Type; So The Above `TL2 'Can Be Defined As: TypeList > tl2;

This works. But it turns out the best value is actually something like `typelist '. This value is much more convenient to use as will illustrated in future installments. Here I would like to rationalize it a little bit through an analogy to the singular value for a pointer in normal programs It is known that 0 is used as the singular value for pointers in normal C programs This value has two basic characteristics:.. first, it is a legal pointer value, which means it can be assigned to any pointer object; second, it semantically means "nowhere" in the memory in analogy, what are needed for a singular value for the pointer in typelist are the following: first, it is a legal typelist value, which means that. itself has to be a typelist;. second, it semantically means there is no node after itself Obviously, `typelist 'satisifies these two requirements much better than` null_type'.A macro-free typelist generatorGenerating a typelist as the Examp le shown above is tedious and error-prone. Easier ways are definitely wanted. In some libraries, such as the famous LOKI, this task relies on the (evil) C macro with ugly interface. We want to do it in a much better way . especially without all the hassle of C macros More specifically, the challege is to implement the most clean and easy-to-use generator with an interface like: typedef gen_typelist :: type tl0; // TL0 == Typelist >>>> typedef gen_typelist :: type tl1; // TL1 == Typelist >>>>

This challenge comes with two problems to be solved:??. First, how to let the `gen_typelist 'accept variable number of template arguments Second, how to get the wanted typelist The C language supports default values ​​for template parameters Employing this, we can make a template to behave as if it can accept variable number of template arguments For example, template struct gen_typelist {};. typedef gen_typelist t1; // OK. typedef gen_typelist t2; // OK.typedef gen_typelist v3;. // OKThis language feature can really be used to solve the first problem Of course, there is an upper bound for the number of template arguments, but it is not a big problem if the template has a large number of parameters. Now we get all the types for constructing the typelist in `gen_typelist '. The least thing we can do is to construct a typelist from the given Types in a bruteforce way. To Continue the Abo ve `gen_typelist 'example, we get: template struct gen_typelist {private: typedef typelist >> crude_tl_; public: }; '. to do this, we process `crude_tl_' This typelist is crude in the way that some` null_type '. are possibly included Next, we need to further refine it to remove all `null_type' except for the` null_typelist with a Meta-Program Defined As Follows: Template

转载请注明原文地址:https://www.9cbs.com/read-87282.html

New Post(0)