How to Implement template functions in C:
Various templates implemented in C language may have different usage forms.
Taking sum as an example, C ++ template can be written as follows:
Template <class T, class r> r sum (const T * array, int N)
{
R Sum = 0;
For (INT I = 0; I <n; ++ I)
Sum + = I;
Return sum;
}
If it is not a built-in type, the template implicitly requires the R: Operator + = (t) operator available.
1.
Use function pointer As functor substitution
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 <n; ++ I)
Self-> Add (self-> sum, array + I * Self-> elemsize );
}
Use Time:
.....
Void addint (char * R1, const char * R2)
{
* (Long *) R1 + = * (int *) R2;
}
Addclass = {addint, 2, 0 };
Int array [100];
Read (array );
Sum (& addclass, array, 100 );
.....
2.
Use macros as functor replacements
# Define gensumfun (sumfunname, add, rettype, elemtype )/
Rettype sumfunname (const elemtype * array, int N )/
{/
Rettype sum = 0 ;/
For (INT I = 0; I <n; ++ I )/
Add (sum, I );/
Return sum ;/
}
Use Time:
# Define addint (x, y) (x) + = (y ))
Gensumfun (sumint, addint, long, INT)
.....
Int array [100];
Read (array );
Long sum = sumint (arrays, 100 );
.....
3.
All replaceable parameters are macros.
At least one additional file (implementation file) is required as impsum. c
/* Impsum. C */
Rettype funname (const elemtype * array, int N)
{
Rettype sum = 0;
For (INT I = 0; I <n; ++ I)
Add (sum, I );
Return sum;
}
Use Time:
# UNDEF rettype
# UNDEF funname
# UNDEF elemtype
# UNDEF add
# Define addint (x, y) (x) + = (y ))
# Define rettype long
# Define funname sumint
# Define elemtype int
# Define add addint
# Include impsum. c
.....
Int array [100];
Read (array );
Long sum = sumint (arrays, 100 );
.....
4.
Summary:
The first method is easy to trace and debug, but inefficient. It is suitable for Variable Functions (function pointers) with low efficiency requirements, but the possibility of program errors is high (complicated ), the template function (SUM) is complex, and the template parameters are also complicated (ADD.
The second method is highly efficient, but it is difficult to trace and Debug. This is especially true when template functions and template parameters are complex.
The third method I came up with was just a few days ago. I think it is the best. When the template parameter (ADD) is complicated, you can use a function (or the second method ), macros can be used in simple scenarios and are easy to debug. The template functions are complex, and the template parameters are superior when they are simple. However, it may be a little complicated.
In general, there is no need to do such painstaking work, and everything is done by the compiler. However, when I develop a file system, because it is based on a rare platform, there is no available c ++ compiler, there are several functions, except for the different types (uint16 and uint32), the function itself is very complex (more than two hundred lines of code), unlike several parameterized macros ). Copy several similar function copies, which is very annoying to maintain. We need such a programming model, so we will share it and discuss it together.