Deep into C ++ function template

xiaoxiao2021-03-06  105

Deepen C function template [original] zengyi820 2003-07-01

Chapter 10 Function Template Chapter 10 Function Template

This chapter is outlined: 1. What is a template 2. Define and use the Template (Template Argument Deduction) 4. Reference Template instance how to specify template arguments (how explicit template arguments can be specified when referring to a function template instantiation) 5. initialization template compiler program and the requirements of the organization (how the compiler instantiates templates and the requirements this imposes on the organization of our specializations programs) 6. the definition of function template instantiations (how to define a specialization for a function template instantiation) 7. how heavy load and parsing function template work (how function templates can be overloaded and how overload resolution involving function Templates Works 8. Name Resolution in Function Template DEFINitions 9. Function Template Defines the definition of how function templates can be defined in namespaces 10.1 Function Template (Function Template Definition) ) (1) Problems taken by strong type language: Strong type language requires our different types of functions to different types of parameters

INT min (int A, int b) {?? RETURN A

?? We tried to solve this problem with the macro extension facility of the pre-processor

#define min (a, b) ((a) <(b)? (a): (b))

There is a problem with such a working mechanism: We are in parsing the working mechanism of the multi-processor's macro expansion facility is not the call to the function, but a simply providing parameter replacement, the two parameter values ​​are calculated twice, once compared A, b. Once is the return value calculation of the macro. ?? Use the function template to retain the semantics of the function definition and function call (before the function call is only calculated once), the function of its core algorithm is: programmers can parameterize all or part of the function interface, and functions The body remains unchanged. (2) The implementation of the "The PrinciPle Of Function Template" function remains unchanged on a set of instances, each instance processes a unique data type, such a function can be constructed into a function template

Template

Type min (Type A, Type B) {

RETURN A

}

Int main () {

// ok: int, int, int, in (int, int); min (10, 20);

// OK: double min (double, double);

MIN (10.0, 20.0);

Return 0;

}

(3) Function Template DEFINITION TEMPLATE is always placed in the top of the template definition and the declaration of the template parameter table, cannot be empty, the template parameters have two categories: template type parameters (Template Type Parameter) List: The keyword class or TypeName (the same meaning as the meaning in the parameter table of the function template) is added. The identifier (such as the TYPE above) can be any name. When the template is instantiated, the actual built-in or user-defined type will replace the type parameters of the template. Template type parameters are used to declare variables and mandatory transitions. Effective template - Particular: int, double, char *, Vector

List

*

Template non-type parameters: consists of a normal parameter declaration, which represents a potential value, which is a constant in template definitions.

Template

TYPE min (Type (& Arr);

Size is the non-type parameters of the function template, and when the length of the ancestors pointed to by Arr, when the function min () is instantiated, the size will be replaced when compiled. Template non-type parameters are commonly used in the array declaration to specify the magnitude of the array or as an initial value of an enumeration

Calling function and taking function addresses are two types of use of functions

(4) Several problems should be noted in the definition of function templates

a. If you declare the objects, functions, or types of the template parameters in the global domain, the global name will be hidden, the following block:

Typedef Double Type; Template

TYPE MIN (Type A, Type B)

{

TYPE TMP = a

Return TMP;

}

b. Objects or types declared in the function template definition cannot be with the template parameters

Template

Type min (Type A, Type B) {

Typedef double type; // This is re-declared the template parameters, so it is an error.

TYPE TMP = a

Return TMP;

}

c. Template type parameter name can be used to specify the return value of the function template

Template

T1 min (t2, t3);

T1 indicates the type of function min () return value, T2 and T3 indicated by the parameter type.

d. Template parameter name is not re-name in the same template, but can be repeated in different function template declarations.

e. Each template type parameter must have a keyword Typename or Class in front, both can be interchangeable.

f. Template function is declared as inline or extern format: indicator is to be placed behind the parameter table

correct:

Template

inline

Type min (Type, Type);

error:

Inline Template

Type min

Int);

Let's take a look at the two exercises in C Primer in classic textbooks to deepen consolidation.

It is pointed out which definitions of the following function template are wrong, which is true, and correct it

(a) Template

Void foo (t, u, v);

Template definition should be Template

Based on: Each template type parameter must have a keyword TypenAMe or Class in front, both can be interchangeable. (b) Template

T foo (int * t);

correct

(c) Template

T1 foo (t2, t3);

correct

(d) inline template

T foo (t, unsigned int *);

It should be:

Template

inline

T foo (t, unsigned int *);

Indicator: The template function is declared in the format of the inline or extern, the indicator is placed behind the parameter table.

(e) Template

Void foo (myt, myt);

The template parameter name is not replayed in the same template.

Can be changed to:

Template

Void foo (myt, myt);

(f) Template

Foo (t, t);

correct

(g) TYPEDEF CHAR CTYPE; TEMPLATE

CType Foo (CType A, CType B);

correct

Exercise 10.2 What is wrong in the following template repeat declaration, why?

(a) Template

TYPE BAR (TYPE, TYPE);

Template

TYPE BAR (TYPE, TYPE);

(b) Template

Void Bar (T1, T2);

Template

Void Bar (C1, C2);

Both are correct

转载请注明原文地址:https://www.9cbs.com/read-124910.html

New Post(0)