This article is a spanzhang original, its blog address is: http://blog.9cbs.net/spanzhang. Quote or repost, please indicate the source, thank you! ! C meta programming is basically used in template, which is a kind of generic programming. Personally think this thing is not particularly useful in actual development, at least I have not actually used. TML (Type Manipunation Library) is a C Meta Programming library developed by the author, which is written purely for research purposes. If you look at the code, you are not dizzy, indicating that your psychological resistance is stronger! In TML, all the code runs in the compile period, which is only working on the type without performing the data. Similar to the overall structure and Standard Template Library (STL), the TML is manipulating the type in the container while STL puts the data into the container. Of course, I have not fully formed this little girl, even if it is molded, it is impossible to compare with STL. Let's go to TML topics from the principle: 1. Inspiration Source (TML's basis) Take a look at a template class: Template Case <1, A, Case <2, b> > type_list; type_list, 2> :: Ret Return; public: typedef return ret;}; This class has two template parameters, only one type definition is provided in the internal public domain. We translate it into the following format: meta_parameters meta_function somename {meta_var case <1, a, case <2, b>> type_list; meta_var Find :: Ret; Meta_Return: meta_var RET RETURN;}; In this way, the original template parameter turned into a function parameter. The original class name SOMENAME turned into a function name. The original class definition became a function body. The previous private domain turned into a function execution operation. Part of the public domain became the return section of the function. In this way, we can do the following definition (or now you should call the operation): Meta_var Somename :: Return ClsResult; this statement does not generate any memory allocation behavior in the running period, and for the compile period A new type will be generated. OK, the basic thing of TML is so much.
Some of the following macro-TML portion forms the basis of: #define META_EQUAL_TO: public # define META_IS: public # define META_FUNCTION class # define META_PARAMETERS template # define META_RETURN public # define META_VAR typedef # define META_PARAM typename # define META_CONST (name, value) enum {Name = (value)}; 2, statement, calculation, and cyclic TML statements are relatively simple, one of the most basic conditions judgment statements are as follows: meta_parameters meta_function if {meta_parameters meta_function select {meta_return: meta_var dam;}; meta_function select {meta_return: meta_var else ret;}; meta_return: meta_var select :: RET RET;}; implementation Select's specialization to complete the judgment operation. In addition, there is a question of external questions, all of TML is called RET without calling returnz because it is troublesome. TML also some operators, such as: META_PARAMETERS META_FUNCTION OR META_IS UNARY_FUNCTION {META_RETURN: META_PARAMETERS META_FUNCTION EXEC {META_RETURN: META_CONST (RET, (EXEC_UNARY_FUNCTION :: RET || EXEC_UNARY_FUNCTION :: Ret);};}; I estimate that you don't know if you see this code. Some other arithmetic symbols are introduced later. Next, a more headache is a loop. In TML, all cycles are done by type iteration, what is type iteration, hehe, I don't know, just. See the examples below: META_PARAMETERS META_FUNCTION PUSH_FRONT {META_VAR MapList :: NEXT nextMap; META_VAR MapList :: VALUE VALUE; META_VAR MapList :: TAG TAG; META_VAR MapList :: PRIOR_TAG PRIOR_TAG; META_VAR nextMap: : Tag nexttag;