C template meta-programming Author: glory Submitted by: eastvc Date: 2003-12-14 19:50:43 original source: http: //www.royaloo.com/articles/articles_2003/Metaprogramming.htm
Abstract This article describes the origin, concepts, and mechanisms of template programming technology, and introduces the application of template element programming technology in the BLITZ and LOKI libraries. Keyword compilation period Calculation Template Programming Blitz Loki Introduction In 1994, the C Standards Committee presented a code that can generate a current number. The special number of this code is that the current number is generated in the compile period rather than the time of operation, and all the numbers between the two to a set value in the middle of the compiler: // prime number computation by Erwin Unruh Template
The following is the revision given by Erwin Unruh, which can compile the standard C compiler today: // prime number computation by Erwin Unruh Template
A Template Metaprogramming analog Module Programming (Template Multi-program "programming" is a "program metaprogram", which can be edged ",") ". That is, we give the code generation rules, the compiler explains these rules in the compile period and generates new code to implement our expected functionality. Erwin Unruh's classic code did not execute, it only outputs the intermediate calculation result in a manner that compiles error information. Let's take a look at a template programming example - calculate the specified for the given integer: // xy.h // Original touch board Template
What will the situation when we take the XY template local specialization? // xy2.h // Original touch board Template
For example, the common method for summing an array is: // Sumarray.hTemplate
Dot.hmatassign.hmatmatmatmatmatvec.hmetaprog.h product.h Sum.h VECASSIGN.H Let's take a look at the template program in the Blitz library Dot.h file: Template
Class _bz_meta_vectordot {
PUBLIC:
ENUM {loopflag = (i Template Static inline bz_promote (_bz_typename t_expr1 :: t_numtype, _bz_typename t_expr2 :: t_numtype) f (const t_expr1 & a, const t_expr2 & b) { RETURN A [I] * B [i] _bz_meta_vectordot } Template Static inline bz_promote (_bz_typename t_expr1 :: t_numtype, _bz_typename t_expr2 :: t_numtype) F_Value_ref (T_EXPR1 A, Const T_EXPR2 & B) { RETURN A [I] * B [i] _bz_meta_vectordot } Template Static inline bz_promote (_bz_typename t_expr1 :: t_numtype, _bz_typename t_expr2 :: t_numtype) f_ref_value (const t_expr1 & a, t_expr2 b) { RETURN A [I] * B [i] _bz_meta_vectordot } Template Static Inline BZ_Promote (_BZ_TYPENAME T_EXPR1 :: T_NUMTYPE, P_NUMTYPE2) Dotwithargs (const t_expr1 & a, p_numtype2 i1, p_numtype2 i2 = 0, P_numtype2 i3 = 0, p_numtype2 i4 = 0, p_numtype2 i5 = 0, p_numtype2 i6 = 0, P_numtype2 i7 = 0, p_numtype2 i8 = 0, p_numtype2 i9 = 0, p_numtype2 i10 = 0) { RETURN A [I] * i1 _bz_meta_vectordot (A, I2, I3, I4, I5, I6, I7, I8, I9); } } Template <> Class _bz_meta_vectordot <0, 0> { PUBLIC: Template Static inline _bz_meta_nulloprand f (const t_expr1 &, const t_expr2 &) {return_bz_meta_nulloperand (); Template Static inline _bz_meta_nullopernd Dotwithargs (const t_expr1 & a, p_numtype2 i1, p_numtype2 i2 = 0, P_numtype2 i3 = 0, p_numtype2 i4 = 0, p_numtype2 i5 = 0, p_numtype2 i6 = 0, P_numtype2 i7 = 0, p_numtype2 i8 = 0, p_numtype2 i9 = 0, p_numtype2 i10 = 0) { Return _bz_meta_nulloperand (); } } This code is much simpler than it looks. _bz_meta_vectorDot Template uses a temporary variable loopflag to store assessment results of each step, and use a fully transmissive version as a condition for recursive end. It should be noted that, like almost all of the programs, this temporary variable is played in the compile period and will be optimized from the run code. Todd is the main author of the BLITZ numerical number library. This library (as well as MTL and Pooma, etc.) illustrations template programs can bring us more efficient numerical computing performance. Todd claims that BLITZ can be compared with the corresponding Fortran library. Loki Base Library: Model of Mechanization Template Programming Technology The value of the template element program is only in high performance value calculations? Not only that. The LOKI library is known for the C community in the pantage model. It is very secluded in the Typelist component. Typelist is an indispensable infrastructure that implements generic models such as Abstract Factory, Visitor. Just like the C standard library component std :: list provides the operation of a set of values, Typelist can be used to manipulate a set of types, which is very simple (taken from the Loki library Typelist.h unit): Template Struct Typelist { Typedef t headad; Typedef u tail; } Obviously, Typelist does not have any status, and does not define any operations only to carry type information, which is not intended to be instantiated, so any processing for TyPelist will inevitably occur in the compile period rather than the operation period. TypeList can be expanded in unlimited because the template parameters can be any type (including other exclusions of the template). E.g: TYPELIST It is a Typelist containing three types of char, int, float. According to the LOKI's convention, each Typelist must end with NULLTYPE. NULLTYPE effect is similar to "/ 0" of traditional C strings, which is declared in the NullType.h file of the Loki library: Class NullType; NullType only declares, no definition, because the Loki library will never need to create a NullType object. Let's take a look at the indexof template program, which can find the location of a given type in a TypeList (from the Typelist.h unit of the Loki library): Template Struct indexof; Template Struct Indexof { ENUM {Value = -1}; } Template Struct Indexof { ENUM {Value = 0}; } Template Struct Indexof { Private: Enum {temp = indexof PUBLIC: Enum {value = (TEMP == -1? -1: 1 TEMP)}; } IndexOf provides an original template and three local specialization. The algorithm is very simple: if TLIST (is a Typelist) is a nulltype, the value is -1. If the head of TLIST is T, Value is 0. Otherwise, IndexOf is applied to the tail and T of the TLIST, and the evaluation result is placed in a temporary variable TEMP. If the TEMP is -1, the value is -1, otherwise Value is 1 TEMP. In order to deepen your understanding of template programming technology used by Typelist, I stripped from the Loki library out of the following code, put in a TypelistLite.h file: // TypelistLite.h // Declaration NullType Class NullType; // Typelist definition Template Struct Typelist { Typedef t headad; Typedef u tail; } // Indexof definition // indexof original template Template / / Local-specific version of NullType Template Struct Indexof { ENUM {Value = -1}; } // For the "TLIST head is the local special version of the T" we want to find Template Struct Indexof { ENUM {Value = 0}; } // Treat part of the TLIST tail Template Struct Indexof { Private: Enum {temp = indexof PUBLIC: Enum {value = (TEMP == -1? -1: 1 TEMP)}; } The test procedure is as follows: // TypelistLite_test.cpp # include #include "typelistlite.h" // Customize Type Royal Class Royal {}; / / Define a TyPelist with char, int, royal and float TypedEf Typelist int main () { Std :: COUT << "Indexof Std :: COUT << "Indexof Std :: COUT << "Indexof } The program output is as follows: Indexof Indexof Indexof Conclusion Template programming techniques are not all advantages. For example, when template program compiles, the code size generated by the programming program is more than the ordinary program, and usually this program is difficult than the conventional procedure. many. In addition, for some programmers, the algorithm may be a bit abstraction in a class template. The cost of compiling time is exchanged for outstanding runtime performance. In general, a meaningful program is always over far exceeding the number of compilation times (or compile time). Bring better experiences for users of the program, or for higher performance, strict numerical values, which is worthy of programmers to pay such a price. It is difficult to imagine template programming technology to become every normal for every ordinary programmer. In contrast, like Blitz and Loki, template programs should always be packaged inside the library. For users of the library, it should be transparent. The template program can (also) use as the kernel of conventional template code, for critical algorithms to achieve better performance, or to achieve special effects for special purposes. Template element programming technology is officially unveiled in Todd Veldhuizen Unsing C Template Metaprograms Paper. This article first published in May 1995 C Report Journal, Later, Stanley Lippman Editor C gems is included in the book. The link to this article is given in the reference, which also describes many of the contents not described herein. David Vandevoorde and Nicolai M. Josuttis C Templates: The Complete Guide spends a whole chapter to introduce template programming technology, which is also the reference material of this article and should also be used as your supplementary reading materials. Andrei Alexandrescu's genius book MODERN C Design: Generic Programming and Design Patterns Applied Chapter 3 Typelists has more detailed descriptions for Typelist. references 1. David Vandevoorde, Nicolai M. Josuttis, C Templates: The Complete Guide, Addison Wesley, 2002 Andrei Alexandrescu, Modern C Design: Generic Programming and Design Patterns Applied, Addison Wesley, 2001.3. Hou Jie Yu Chun Jing translated, "C Design New Thinking", Huazhong University of Science and Technology Press, 2003. . 4. Todd Veldhuizen, Template Metaprograms, http://osl.iu.edu/~tveldhui/papers/Template-Metaprograms/meta-art.html 5. Todd Veldhuizen, C templates as partial evaluation (PEPM99), http: / / 斯L.iu.edu/~tveldhui/papers/pepm99/. 6. Erwin unruh, prime number, http://www.erwin-unruh.de/primorig.html. 7. Erwin Unruh, Prime Numbers (primzahlen), http://www.erwin-unruh.de/prim.html. 8. Blitz , http://www.oonumerics.org/blitz. 9. loki, http://sourceforge.net/projects / loki-lib. 10. Pooma, http://www.pooma.com. 11. Mingw - minimalist gnu for windows, http://sourceforge.net/projects/mingw.