-
template
You can have as many classes as you want.
Then there's the generic function definition.
The format is generic, all that needs to be changed is the number of types and function definitions depending on the situation.
-
The template definition starts with the keyword template, followed by the template parameter table, which is a list of one or more template parameters enclosed in angle brackets, and the parameters are well separated.
When using a function template, the compiler infers which (or which).
The template parameters are bound to the template parameters. Once the compiler has determined the actual template arguments, it is said to have instantiated an instance of the function template. It can be simply thought that the compiler combines the arguments with the template and generates the desired function.
If you can call it like this: selectsort(mycompare,myarray,6); The myarray can be integer, floating-point, or character-based.
The algorithm module written through the above example is transformed from a single function to multiple functions, which improves reusability, and at the same time leaves an interface for the user, which is more interactive and flexible.
Summary of function templates.
Let's summarize the format and precautions for writing a function template (copied from the textbook):
Function templates. Form:
template
function
or: template
function
More general form:
template
function
In these forms, typeparam....is a universal type form parameter; Each specifier is a keyword typename or class; And function is the prototype or definition of this function. Note:
The word template is a C++ keyword that specifies that it is followed by a pattern of a function, rather than an actual prototype or definition of a function.
The keywords typename and class can be used interchangeably in a list of type parameters.
Relative to the "normal" form parameters (as well as the actual parameters) appear in parentheses, and type parameters appear in angle brackets.
Unlike regular functions, a function template cannot be spread across multiple files, i.e., its prototype cannot be placed in a header file and its definition in another implementation file. The function templates must all be placed in the header file.
A function template is simply a pattern that describes the production of different functions based on a given actual type. This process of creating a function is called instantiation.
In the generic form, each type of parameter must appear at least once in the list of parameters of the function. The reason for this is that the compiler relies only on the type of the argument in the function call to decide what type to bind to the type argument.
-
Function templates
Using a function template, you can specify a set of functions that are based on the same but handle different types or classes, for example:
template void myswap( type& a, type& b )
This paragraph defines a family of functions to swap the parameter values of the function. From this template you can generate a series of functions that can swap not only integers, long integers, but also user-defined types, and even myswap functions if the class's constructor and assignment operators are properly defined.
In addition, function templates can prevent you from swapping different types of objects, because the compiler knows the types of arguments a and b at compile time.
You can call a function template function just like a normal function; No special syntax is required. For example:
int i, j;
char k;
myswap( i, j );//ok
myswap( i, k );//error, different types.
You can externally describe the template parameter of a function template, for example:
templatevoid f(t)
void g(char j)
When the template parameter is specified externally, a normal fixed type conversion converts the function parameter to the corresponding function template parameter. In the example above, the compiler converts (char j) to an integer.
-
Function Templates" introduces the concept of function templates, their uses, how to create function templates and how to use function templates.
Building formwork is a common material in construction and is a building structure that meets the needs of temporary support. There are many types of building templates, such as: wood building template, clear water building template, steel building template, aluminum alloy building template, etc., these four are more common building templates. >>>More
1. Topic (state the title of the lesson).
2. Teaching objectives (or teaching requirements, or teaching objectives, which describe the teaching tasks to be completed in this lesson). >>>More
zip file package, automatic decompression without worrying, card swiping method: >>>More
1. Before creating a resume, determine who is the reader in advance. A resume is then created based on a defined reader. >>>More
4.Background: It is the historical situation and the environmental conditions of the news. >>>More