Follow me to learn c++ advanced articles - template metaprogramming seven typelist

1. typelist

how to say? Typelists is actually proposed in "Modern c++ Design". The name of typelist has a lot to do with list. Those who have studied c++ development will definitely know several commonly used data structures vector and list in the STL library. list is a generic representation of a linked list. So typelist is the "generic type" of the type, and of course this generic type is quoted. After all, there is no such definition in metaprogramming, so I say it for easy understanding.
And typelist doesn't look very good-looking, yes, it just doesn't look good, and it doesn't conform to people's cognitive habits of lists. Take a look at the code:

template <typename T,typename U>
struct Typelist
{
 typedef T Head;
 typedef U Tail;
};
typedef Typelist<char,Typelsit<int,Typelist<long,Typelist<short,NullType>>>> Ex;

It needs to be explained here that in the template processing of the early version, the compiler is a bit ambiguous and does not support consecutive template identifiers ">>", and a space must be added between them. But now there is no problem with the higher version of the compiler. If you encounter this kind of problem, you should either use an advanced IDE or add a space.
Pulling back, it seems that this thing has nothing to do with the list at all, so the book says that it needs to be packaged, and packaging can solve the problem, just like the real world, the big sister of 50, after makeup, becomes the young lady of 15 , look at the code:

#define TYPELIST_1(T1) Typelist<T1,NullType>
#define TYPELIST_2(T1,T2) Typelist<T1,Typelist(T2)>
#define TYPELIST_3(T1,T2,T3) Typelist<T1,Typelist(T2,T3)>
#define TYPELIST_4(T1,T2,T3,T4) Typelist<T1,Typelist(T2,T3,T4)>
...
#define TYPELIST_50(...) ...

After understanding the above code, you can basically grasp what typelist is.

Two, the essence of typelist

Then in the early c++ template programming, this technique can be used to realize different types of "generics", the essence of which is to use the template template parameters in the previous article. Its ultimate purpose is to dynamically realize the creation of various objects according to the type (just like the factory mentioned in the book). This is very useful for some cases (such as when dealing with virtual functions).
In other words, the above code generation process is expressed in the form of template parameters of the template.

typedef Typelist<char,Typelsit<int,Typelist<long,Typelist<short,NullType>>>> Ex;

After writing here, the motivation and generation mechanism of Typelist can basically be clarified. So, is there any way to improve this way of implementing Typelist through more complicated techniques? That must be there. But in the new standard of C++, use the variable parameter of the template mentioned above:

//声明
template<typename ... Args> class TypeList;
//偏特化实现
template<typename Head, typename ... Tail> class TypeList<Head, Tail...>;

//实现方法
template <typename T,typename U>
struct TypeNode
{
  typedef T Head;
  typedef U Tail;
};
template<typename Head, typename ...Tails>
struct TypeList
{
    using Result = TypeNode<Head, typename TypeList<Tails...>::Result>;
};

template<typename arg>
struct TypeList<arg>
{
    using R = TypeNode<H, NullType>;
};

So if you want to save trouble, you have to make progress from the root.
The internal implementation of Typelist such as search and erasure is not analyzed here, after all, it is very clear in the book.

3. Automatically generated classes of Typelist

In MFC, macro expansion was used to automatically generate classes, which is very complicated and almost no one likes it. Then you can also generate classes with Typelist. Let's see an example:

template <class TList,template<class> class Unit>
class Gen;
template <class T1,class T2,template<class> class Unit>
      :public Gen<T1,Unit>,public Gen<T2,Unit>{};
class Gen;
template <class AT,template<class> class Unit>
class Gen:public Unit<AT>;
template <template<class> class Unit>
class Gen<NullType,Unit>{};

Using Typelist can generate code in a more skillful way of template template parameters. It is no wonder that Mr. Hou Jie said that this technique is a relatively deep template technology. If you are interested, you still need to learn more.
Classes can be automatically generated, and you can add your own materials, such as restricting the order of inheritance and the conditions of inheritance, all of which can be operated in the implementation of the above model.

Four. Summary

Knowing why, it is convenient to use this technique in metaprogramming. Especially for beginners, if you look at some great codes, you will find blockbuster macros plus variable parameters and other techniques. If you search, there are very few people who engage in this kind of development, so you may end up giving up at the beginning. But if you start from these basic aspects and continue to disassemble, you will suddenly find that the metaprogramming of templates can still go deep into it boldly.
Work together with you!

Guess you like

Origin blog.csdn.net/fpcc/article/details/129261414