C++ Template Beginner

Generic programming

Among the following exchange functions, they only differ in type. How to implement a universal exchange function?

void Swap(int& left, int& right)
{
    int temp = left;
    left = right;
    right = temp;
}

void Swap(double& left, double& right)
{
    double temp = left;
    left = right;
    right = temp;
}

void Swap(char& left, char& right)
{
    char temp = left;
    left = right;
    right = temp;
}

Although it is possible to use function overloading, there are several disadvantages:
        1. The overloaded functions are only of different types, and the code reuse rate is relatively low. Whenever a new type appears, the user needs to add the corresponding function;
        2. The maintainability of the code is relatively low, and one error may cause errors in all overloads.

Can you tell the compiler a model and let the compiler use the model to generate code according to different types?​ 

        If there can be such a mold in C++, by replacing different fonts (types) in this mold to obtain different templates (that is, generating specific types of code), it will save a lot of hair and time. Coincidentally, the trees have already been planted by our predecessors, so we only need to enjoy the shade here. With the idea as shown above, we can also create such a template! There is generic programming.
Generic programming: Writing type-independent universal code is a means of code reuse. Templates are the basis of generic programming.

Templates are divided into:

Function template:

Function template concept:

        Function template represents a function family, which has nothing to do with type, is parameterized when used, and generates a specific type version of the function based on the actual parameter type.

Function template format:

格式:template<typename Type>
           template<class Type>

Note: typename is used to define template parameter keywords, and class can also be used (remember: struct cannot be used instead of class)

Function template principle:

        A function template is a blueprint. It is not a function itself. It is a mold that the compiler uses to generate a specific type of function. So in fact, the template is to hand over the repetitive things that we should do to the compiler.

        During the compiler compilation phase, for the use of template functions, the compiler needs to deduce and generate the corresponding type of function for calling based on the type of the actual parameters passed in.

For example: when using a function template with a double type, the compiler determines T as a double type through deduction of the actual parameter type, and then generates a code that specifically handles the double type. The same is true for character types.

Function template instantiation: 

        When a function template is used with parameters of different types, it is called instantiation of the function template.

        Template parameter instantiation is divided into: implicit instantiation and explicit instantiation.

Implicit instantiation: Let the compiler deduce the actual type of the template parameter based on the actual parameter

 Explicit instantiation: specify the actual type of the template parameter in the <> after the function name

Matching principles for template parameters:

1. A non-template function can exist at the same time as a function template with the same name, and the function template can also be instantiated as the non-template function;
2. For non-templates Functions and function templates with the same name, if other conditions are equal, will give priority to calling non-template functions when calling and will not generate an instance from the template. If the template can produce a function with a better match, then the template will be selected;

3. Template functions do not allow automatic type conversion, but ordinary functions can perform automatic type conversion.

 Can be understood as:

class template

The definition format of class template:

template<class T1, class T2, ..., class Tn>
class class template name
{         // Definition of members within the class };

 Class template instantiation:

#include <iostream>
using namespace std;

template<class T>
class Stack
{
public:
	Stack(int n = 4);

	~Stack()
	{
		cout << "~Stack()" << endl;

		delete[] _a;
		_a = nullptr;
		_top = _capacity = 0;
	}

	void Push(const T& x)
	{
		//...
	}

private:
	T* _a;
	int _top;
	int _capacity;
};

//类模版不能将定义和声明在不同的文件
//类外定义
template<class T>
Stack<T>::Stack(int n)
{
	cout << "Stack(int n = 4)" << endl;

	_a = new T[n];
	_top = 0;
	_capacity = n;
}

int main()
{
	//在普通的类中:类名就是类型
	//在类模版实例化的类中:类名不是类型,类名<数据类型>才是类型
	Stack<int> st1;//显示实例化
	Stack<double> st2;

	//显示实例化的类型不同,它们就是不同的类
	//st1 = st2;//一个是Stack<int>  一个是Stack<double>

	return 0;
}

        Class template instantiation is different from function template instantiation. Class template instantiation needs to be followed by <>, and then the instantiated type can be placed in <>. The class template name is not a real class. The result of instantiation is the real class.

The above is my personal learning insights and analysis of the learning process. You are welcome to discuss and communicate in the comment area!
If this article is helpful to you, please support it.
Thank you guys for your support! Thank you guys for your support! Thank you guys for your support!

                                              

Guess you like

Origin blog.csdn.net/weixin_71964780/article/details/134425017