Function Templates are advanced concepts in C , and the primary users are not necessarily realized even if they have used the function templates in some standard libraries.
As a strong type of language C , many times often restrict some stronger language functions, such as in foot languages such as Perl / PHP, the type of fuzzy is the universal algorithm.
(Such as MIN / MAX) provides better support, the same algorithm can be applied to various different data types, and these differences are handed over to the interpreter, which is undoubtedly provided to users.
Convenience. In C, it is generally only possible to achieve a similar effect by using macros, but it is obvious that this is not a type of security and a safe way. And C must write functions for various types (such as
The overload function), a better way is to use the function template.
Template
Temp_par is a template type parameter. It represents a type or a template non-type parameter (Template Nontype Parameter).
It represents a constant expression.
Template Type Parameter is composed of a identifier by Class or TypeName, which is the same as the meaning of these two keywords in the template parameter table of functions.
The parameter name represents a potential built-in or user-defined type, and the template parameter name is selected by the programmer.
Template
Glorp MIN (Glorp A, GLORP B) {
RETURN A
}
Template Nontype Parameter is constant during the template instantiation
Like a non-template function, the function template can also be declared as inline or extern should put the indicator behind the template parameter table instead of the keyword template, such as:
Template
inline
Glorp MIN (Glorp A, GLORP B) {
RETURN A
}
To analyze the template definition, the compiler must distinguish the expression that the expression is a computational expression or a declarative type. It is not easy for the compiler.
For example, if the compiler encounters an expression PARM :: Name in the template definition, and the PARM template type parameter represents a class, then the Name reference must be
Is a type member of PARM?
Template
PARM Minus (Parm * Array, U Value)
{
PARM :: Name * P; // This is a pointer declaration or multiplication
}
Obviously, the compiler does not know if the Name is a type because it can only find the definition of the classes represented by PARM after the template is instantiated. In order to make the compiler to analyze the template
Definition, the user must indicate which expressions are type expressions. Tell the compiler An expression is a type expression mechanism to add keyword Typename before expressions.
For example: If we want the expression PARM :: Name of the function template minus (), it is a type name, so that the entire expression is a pointer declaration that we should modify as follows.
Template
PARM Minus (Parm * Array, U Value)
{
TypeName Parm :: Name * p; // ok: Pointer declaration
}
This is actually the initial reason for C keyword Typename, now mainly used in the template parameter table to indicate a template parameter is a type.
Function template is instantiated when it is called or taken off
When the template is determined during the Template Conversion Template, the multi-purpose algorithm of the return type template for the function template instance is as follows.
1 Check each function intern parameters to determine the template parameters that appear in the type of each function parameter
2 If the template parameter is found, the corresponding template of the corresponding template is pushed by checking the type of the function.
3 Function Parameter Type and Function The real-constricted type does not have to match. The following type conversion can be applied to the functional parameters to convert it into a corresponding function parameter.
A. Left value conversion (array to pointer, function to pointer)
B. Limited modified conversion (const, volatile)
C. Transition from derived class to base class type.
4 If the same template parameters are found in multiple function parameters, the templates that are first arguing from each corresponding function must be the same.
As you can see here, the prunity of the template-argument does not allow ordered standard conversion, because which function is determined, not which function is determined
For example: Sum
At this point, there is no need to deduce the template, and the type of function parameters is fixed. When the function template is explicitly specified, the function is converted into the corresponding function parameters.
Types can apply any implicit type conversion (of course, including ordered standard conversion)
Explicit template should only be used in the context that completely needs them to solve the second meaning, or in the context that the template is unable to be thrown.
Including Inclusion Model and Separation Model,
For the former, each template contains a template definition in an instantiated file, and the definition is usually placed in the header file, just as the Inline function definition. weakness is,
There is no separation and declaration, and may result in multiple recording, while causing multiple instantiation (this can be solved by displaying instantiation).
For the latter, the template function is implemented in a separate implementation file, and the header file is only the declaration of the template function. But the definition of the template function is required to appear the export keyword.
Such benefits are separating interfaces and implementations. The disadvantage is to carefully plan code, try to prevent Exports in multiple files from appearing in the same function template, which may result
Link error, another regret is that there are very few compilers to support Separation Model (maybe GCC or VC7 has been supported)
In order to be a template more practical, standard C provides this mechanism to help users determine the timing of the function instantiation, the usage is as follows:
Template
TYPE SUM (Type Op1, INTOP2) {/ * ... * /}
// Explicit instantification statement
Template Int * SUM
Then INT * function is explicitized. For a given function template instance, explicit instantification declaration can only occur once in a program, and the explicit instantiation statement is
The definition of the function template in the file must be given. If this definition is not visible, the explicit instantiation statement is wrong.
Explicit instantiation statements are combined with another compilation option. This option presses implicit instantiation of the template in the program. The name of the option is different from the compiler.
(I feel that this mechanism is not very useful, I don't know what the GCC option is)
Although the template has made a lot of beneficial work for us, sometimes we don't want this template function to instantize all our types, usually because some types
If exception is processed, there is a greater benefit to performance and functionality. For example, for a relatively equal function, for ordinary types (int, bool), their operating bases
This is the comparison of Bitwise, and for the string type (char *) of the C style, we need to compare MEMBERWISE. Therefore, C provides template display specialization mechanism to give us a "overload" opportunity.
E.g:
/ / Universal template definition
Template
Tmax (T T1, T T2) {
RETURN (T1> T2? T1: T2);
}
// const char * Explicit Specialization:
// Cover an instance from a general template definition
Typedef const char * pcc;
Template <> PCC MAX
RETURN (STRCMP (S1, S2)> 0? S1: S2);
}
Note: Before using the function template in the source file, you must make a declaration before explicitly.
Typically, the declaration of the template explicit-specific declaration is included in each file that requires the type real-name function template that needs to be special. The explicit special declaration should be placed in the header file.
And contain this file in all programs that use the function template
The function template can also be overloaded, just name analysis, due to the joining of the template, the introduction of the motion process, it is a bit complicated. The main steps are as follows:
1. When the compiler see a function call point, first determine the function of all the call points of all the same name. In addition to the ordinary overload function, the candidate function is also plus those
Introducing the successful template function, if there is a template, it is actually a candidate function.
2. The optional function set (according to three standard conversion) is determined, which is consistent with the normal overload analysis.
3. Take a variety of conversions, in the last remaining function, if there is a normal function and the template function, only the normal function is taken, if the ordinary function is not limited
One, compile error. Obviously, if there is no template function in the last remaining function, then the result is consistent with the general overload function, that is, if more than one function is full.
Foot requirements, compile error.
There is a problem here that may cause confusion. Why is a normal function when there is a normal function and a template function, give priority. This is to cope with this situation.
When we have to take an explicit template for a type of explicit template, we do not modify multiple function calls in the entire file, use a normal function to declare it.
In the normal function, the display template is displayed in the normal function. As shown below:
// Function template definition
Template
TYPE min (Type T1, Type T2) {...}
// ordinary function
INT min (int A1, int A2) {
MIN
}
Int main () {
// Call a normal function
MIN (Ai [0], SS);
}
Imagine if you need to call MIN
Rules, the programmer uses popular editor software to completely replace all function call points, it is definitely not difficult.
In the template definition, some name symbols are required to be parsed when they need to instantiate the template, which is typically a template's real parameter type. Something can be used when the template is defined.
F. The former is often referred to as the name of the Template parameter (the latter obviously does not depend on the template parameters.
For functions that do not depend on the template parameters, you must declare before defining the template.
For function calls that depend on the template parameters, you must declare before template instantiation.
Therefore, it is obvious that the name that does not rely on the template parameters is declared by the provider of the template library, and the user can only give a statement to the name when needed.
In fact, it is very simple to define the function template in a namespace, just define a normal function in the namespace, before use, or the function you need.
Or bring the entire name space for using.
Both of these are actually there is no essential association.
2004.8.25 Correction of incorrect errors, change certain paragraphs make it more smooth and easy to understand.
2004.8.12 Initial Version RELEASED