Method for implementing template functions in C language:
Templates implemented in various C language may vary in the form of use.
Now uses a summary function SUM as an example, written with C Template as follows:
Template
{
R SUM = 0;
For (int i = 0; i SUM = I; Return SUM; } If it is not a built-in type, the template implicitly requires R r :: Operator = (T) operator available. 1. Use the function pointer as a Functor replacement Typedef struct tagaddclass { Void (* add) (Char * R1, Const Char * R2); Int elemsize; CHAR SUM [MAX_ELEM_SIZE]; Addclass; Void Sum (AddClass * Self, Const Char * Array, Int N) { For (int i = 0; i Self-> Add (Self-> Sum, Array i * Self-> Elemsize); } when using it: ... .. Void Addint (Char * R1, Const Char * R2) { * (long *) R1 = * (int *) R2; } Addclass AddClass = {Addint, 2, 0}; Int arch [100]; Read (array); SUM (& AddClass, Array, 100); ... .. 2. Use macro as a replacement of functor #define gensumfun (Sumfunname, Add, Rettype, ELEMTYPE) / Rettype Sumfunname (const elemType * array, int N) / {/ RETTYPE SUM = 0; / For (int i = 0; i Add (SUM, I); / Return SUM; / } when using it: #define addint (x, y) ((x) = (y)) GensumFun (SUMINT, AddINT, long, int) ... .. Int arch [100]; Read (array); Long Sum = SUMINT (Array, 100); ... .. 3. All replaceable parameters are macro At least one additional file (implementation file) is Impsum.c / * iMPSum.c * / Rettype Funname (const elemType * array, int N) { Rettype SUM = 0; For (int i = 0; i Add (SUM, I); Return SUM; } when using it: #undef rettype #undef funname #undef elemType # undef add #define addint (x, y) ((x) = (y)) #define rettype long #define funname sumint #define elemType Int #define address #include impSum.c ... .. Int arch [100]; Read (array); Long Sum = SUMINT (Array, 100); ... .. 4. Summary: The first method is easy to track debugging, but the efficiency is low, it is suitable for the efficiency requirements for variable functions (function pointers), but the possibility of the program is large (complex), the template function (SUM) is very complicated, The template parameters are also more complex (add). The second method, high efficiency, but it is difficult to track debugging, this is even more complicated in the template function and template parameters itself. The third method is that I have thought of recently, I think it is best, can be used when the template parameter (add) is more complicated (the second can also be the same), simply use the macro, and Easy to debug. The template function itself is complicated, and the template parameters are relatively superior. However, it may be a bit cumbersome. Under normal circumstances, there is no need to do such a work, everything is handed over to the compiler to do it. But when you develop a file system, because it is based on a rare platform, there are several functions available, in addition to the types of the type (UINT16 and UINT32), and several parametric macro Other places are exactly the same, and the function itself is complicated (more than two hundred lines of code). Copy has a few fully similar copies, which is particularly annoying. It is very necessary to programmatically, so this, share it out, and everyone discusses.