C++ programming language study notes about templates

1. Template function

1.1 Basic Concepts

Templates generate code at compile time, and its execution is not macro replacement, but renaming rules

//传参后,模板函数编译时候如下,而不是将Type替换为int
typedef int Type;
void Swap<int>(Type& a,Type& b)
{
    
    
Type tmp=a;
a=b;
b=tmp;
}

And the following N is a macro substitution that replaces a constant,
insert image description here

1.2 Template function push example:

Through the different parameters passed in in the following example, let's analyze why T is deduced at compile time? (Notes have given the results of the deduction)
Example 1:
insert image description here
Example 2:
insert image description here
Example 3:
insert image description here
Example 4:
insert image description here

1.3 Distinguish between full generalization, partial specialization and full specialization

insert image description here

1.4 Note:

①Because the bottom layer of the reference (&) is int * const (constant pointer), after the reference is passed in, the compiler cannot deduce the const after int* , so the compilation fails.
insert image description here
So we need to manually add const after T in the template function
insert image description here

② Template functions can also be overloaded
insert image description here

2. Template class

2.1 Deduction rules for template class compilation

Add the concrete type of T to the class name and
rename the concrete type to T with a typedef inside the class
insert image description here

2.2 There can be non-types, and when our N is a different value, the object type will also be different

For example, the following N is 100 and 10 respectively. After the replacement, two different pairs will be instantiated (the reason is that the member attributes are different, so they are different objects ).

insert image description here

2.3 Compilation of functions in class templates

If the function in the class template is mobilized, the function in the class template will participate in the compilation, and if it is not mobilized, it will not participate in the compilation

So the following will change the value of N, if we do not call it will not detect the error.
insert image description here

2.4 Template type parameters can be assigned default values

insert image description here

2.5 Template type parameters can have multiple

insert image description here

2.6 When is a template type

Arrayiat, because an integer type has been explicitly given, the compiler will instantiate the specific content, then this is a specific type

Array This is a template type, since no explicit type is given, the compiler can't make a judgment about what type to handle.

template<class T>
class Array
{
    
    
   enum{
    
    INIT=10};
   T* data;
   size_t capacity;
   size_t count;
};
int main()
{
    
    
    Array<int>iar;//这不是一个模板类型,因为编译器会把它实例化为下面的代码:
    /*
    class Array<int>
    {
       typedef int T;
       enum{INIT=10};
       T* data;
       size_t capacity;
       size_t count;
    };
    */
    Array//这是一个模板类型
}

2.7 Introduction of Type Extraction Problems

T is a type,

seq is a type that can receive a template type, which must have a template parameter

If in the use of the main function, this situation occurs:

Container<int,Array>contatiner;

Then this will cause Array, which is not a template type, to become a specific design type, so it should become like this: Container<int,Array>contatiner;
insert image description here

How to type extraction, so that T can be clearly identified as int type?

Do not directly replace all T in the program with int, or other specific types, this is boring, so type extraction is required.
insert image description here

Guess you like

Origin blog.csdn.net/m0_54355780/article/details/122892330