C++ generic programming, function templates and class templates

1. Generic programming

Generic programming: writing generic code that is independent of type is a means of code reuse. Templates are the basis of generic programming

For example, movable type printing provides a mold and then prints different characters according to the mold.

Generic programming follows a similar pattern, providing a template, based on which the compiler automatically generates different functions or classes.

2. Function template

2.1 Why there is a function template?

In C language, if we want to exchange two integers, exchange two floating point numbers, or exchange custom types, we must manually write n functions ourselves! ! ! And the function name of each function is different! ! !

void SwapInt(int& a,int& b)
{
	int c = a;
	a = b;
	b = c;
}
void SwapDouble(double& a, double& b)
{
	double c = a;
	a = b;
	b = c;
}

int main()
{
	int a1 = 1, b1 = 2;
	SwapInt(a1, b1);
	cout << a1 << " " << b1 << endl;
	double a2 = 1.1, b2 = 2.2;
	SwapDouble(a2, b2);
	cout << a2 << " " << b2 << endl;
	return 0;
}

Is there a way that we can complete the exchange of various types by writing only one function (template) instead of writing many functions? This is when function templates come on stage.

2.2 How to use function templates

Template format:

template<class T1,class T2,class T3...>

Return value function name (function parameter) { function body }

template<class T>
void Swap(T &a ,T &b)
{
	T c = a;
	a = b;
	b = c;
}
int main()
{
	int a1 = 1, b1 = 2;
	Swap(a1, b1);
	cout << a1 << " " << b1 << endl;
	double a2 = 1.1, b2 = 2.2;
	Swap(a2, b2);
	cout << a2 << " " << b2 << endl;
	return 0;
}

2.3 Instantiation of function templates

The swap we provide here is just a function template, not a real function.When using function templates with different types of parameters, requires the compiler to automatically generate a corresponding function, which is called instantiation of the function template! ! !

The instantiation of templates is divided intoexplicit instantiationandimplicit instantiation

2.3.1 Implicit instantiation

The compiler identifies the actual parameter type and deduce the type of the template parameter.

template<class T>
void Swap(T &a ,T &b)
{
	T c = a;
	a = b;
	b = c;
}
int main()
{
	int a1 = 1, b1 = 2;
	Swap(a1, b1);

	double a2 = 1.1, b2 = 2.2;
	Swap(a2, b2);
	return 0;
}

The type of T is deduced from the parameters passed in. This is called implicit instantiation.

2.3.2 Display instantiation

Specify the specific type passed in the <> after the function name

template<class T>
void Swap(T &a ,T &b)
{
	T c = a;
	a = b;
	b = c;
}
int main()
{
	int a1 = 1, b1 = 2;
	Swap<int>(a1, b1);

	double a2 = 1.1, b2 = 2.2;
	Swap<double>(a2, b2);
	return 0;
}

3. Class template

3.1 Why is there a need for class templates?

When we need two stacks, one stack stores int type and one stores double type, we can only write two different classes ourselves. These two classes have different class names, but except for the type, everything else in the class is the same. of!!!

class StackInt
{
public:
	StackInt(int capacity = 4)
	{
		_a = new int[capacity];
		_top = 0;
		_capacity = capacity;
	}
	~StackInt()
	{
		delete[] _a;
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	int* _a;
	int _top;
	int _capacity;
};
class StackDouble
{
public:
	StackDouble(int capacity = 4)
	{
		_a = new double[capacity];
		_top = 0;
		_capacity = capacity;
	}
	~StackDouble()
	{
		delete[] _a;
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	double* _a;
	int _top;
	int _capacity;
};

int main()
{
	StackInt st1;
	StackDouble st2;
	return 0;
}

At this time, we need to use our class template to solve this problem.

3.2 How to use class templates

Template format:

template<class T1,class T2,class T3...>

class class name { member functions and member variables };

template<class T>
class Stack
{
public:
	Stack(int capacity = 4)
	{
		_a = new T[capacity];
		_top = 0;
		_capacity = capacity;
	}
	~Stack()
	{
		delete[] _a;
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	T* _a;
	int _top;
	int _capacity;
};


int main()
{
	Stack<int> st1;
	Stack<double> st2;
	return 0;
}

3.3 Instantiation of class templates

The instantiation of a class template is different from the instantiation of a function template. The instantiation of a class template is to add <> after the class name and the instantiation type in <>. The class template name is not a real class, but the instantiation result is the real class .

Stack is the class name

Stack<int> is type

Guess you like

Origin blog.csdn.net/qq_73955920/article/details/134675884