Function template in C ++

xiaoxiao2021-03-06  149

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