[c++ practice path] template

template

Generally, when we implement a function, we will use a template, because if the type is hard-coded, a new function will be written next time we use it again. Although overloading can make the name convenient, you have to write every overload A function is very troublesome, so the template is to let the compiler deduce it by itself to generate the corresponding function, so as to realize generic programming.

Disadvantages of overloading:
1. The code reuse rate is low. As long as a new type appears, the function has to be rewritten.
2. Security issues, as long as there is a problem with one overloaded function, all overloaded functions will go wrong.

Generic programming: Writing generic code that has nothing to do with types is a means of code reuse, and templates are the basis of generic programming.

function template

Format

template<class T>
bool Compare(T& t1,T& t2)
{
    
    
	return t1 < t2;
}

instantiate

Implicit instantiation:
It is called implicit instantiation derived by the compiler itself. If the above function is passed in <int, double>, an error will be reported during compilation, because the compiler cannot determine whether T is int or double, and it can display forced conversion or explicitly instantiated.

int a = 10;
double b = 20;

//编译失败
Compare(a,b);

Explicit instantiation:
If the types do not match, the compiler will automatically perform implicit type conversion for the mismatched types, and report an error if it does not work.

int a = 10;
double b = 20;

//显式实例化
Compare<int>(a,b);

class template

Format

template<class T1, class T2, ..., class Tn>
class 类模板名
{
    
    
	// 类内成员定义
};

ps: Functions defined outside the class need to add a class template parameter list:

template <class T>
Vector<T>::func()
{
    
    }

instantiate

Class template instantiation needs to specify the type, such as

vector<int> v;

Among them, the vector 不是type, vector is the type.

functor

As the name suggests, a functor is a function that is imitated by a class. Examples are as follows:

template<class T>
class Compare
{
    
    
public:
	bool operator() (const T& t1,const T& t2)  
	{
    
    
		return t1 < t2;
	}
};

non-type template parameters

Type parameters:
parameters following class/typename, int, double

Non-type parameter:
it can be a specific variable, but it cannot be a floating point number, character, string, etc...

Example:

template<class T, size_t N = 100>
class test
{
    
    
};

template specialization

function template specialization

Why template specialization is needed:
The comparison function above is fine for basic type comparisons, but if it is obviously inappropriate for us to compare the contents of two pointers, and we want to be able to compare the contents of two pointers, we will A specialization can be chosen:
Example:

template<class T>
bool Compare(T t1, T t2)
{
    
    
	return t1 < t2;
}

template<>
bool Compare<int*>(int* t1, int* t2)
{
    
    
	return *t1 < *t2;
}

But note: it must be exactly the same as the parameter list of the template function:

template<class T>
bool Compare(T& t1, T& t2)
{
    
    
	return t1 < t2;
}

template<>
bool Compare<int*&>(int*& t1, int*& t2)
{
    
    
	return *t1 < *t2;
}

class template specialization

full specialization

template<class T1,class T2>
class Data
{
    
    

};

template<>
class Data<int, char>
{
    
    

};

partial specialization

//偏特化
template<class T1>
class Data<T1, char>
{
    
    

};
//进一步限制
template<class T1>
class Data<T1*, char>
{
    
    

};

Template separate compilation

Why is there a problem with template separation compilation:

First, let’s review the process of program compilation:

Explanation: The function declaration can be obtained in the compilation stage, and the compilation can be passed smoothly, but the link cannot be passed, because the function address cannot be found during the link (the function is not instantiated).
insert image description here

Guess you like

Origin blog.csdn.net/m0_73209194/article/details/130466118