C + + Learning_ Initial stage - initial stage of formwork

1. Generic programming

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

2. Function template

2.1 function template concept

The function template represents a function family. The function template is independent of type and is parameterized when used. The specific type version of the function is generated according to the argument type.

2.2 function template format

template<typename T1, typename T2,...,typename Tn>
Return value type function name (parameter list) {}

template<typename T>
void Swap( T& left, T& right)
{
T temp = left;
left = right;
right = temp;
}

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

2.3 principle of function template

Function template is a blueprint. It is not a function itself. It is a mold for the compiler to generate specific types of functions by using. So in fact, the template is to give the compiler the repeated things we should have done.

In the compiler compilation stage, for the use of template functions, the compiler needs to deduce and generate functions of corresponding types according to the input argument types for calling. For example, when the function template is used with double type, the compiler determines T as double type through the deduction of argument type, and then generates a code specially dealing with double type, which is the same for character type.

2.4 function template instantiation

When a function template is used with different types of parameters, it is called instantiation of the function template. Template parameter instantiation can be divided into implicit instantiation and explicit instantiation.

  1. Implicit instantiation: let the compiler deduce the actual type of template parameters according to the actual parameters
cout << Sum(10, 20) << endl;
  1. Explicit instantiation: specify the actual type of the template parameter in < > after the function name
	cout << Sum<int>(10, 12.23) << endl;

2.5 matching principle of template parameters

  1. A non template function can exist simultaneously with a function template with the same name, and the function template can also be instantiated as the non template function.

  2. For non template functions and function templates with the same name, if other conditions are the same, the non template function will be called first during transfer, and an instance will not be generated from the template. If the template can produce a function with a better match, the template will be selected.

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

3. Class template

3.1 definition format of class template

template<class T1, class T2, ..., class Tn>
class Class template name
{
// Intra class member definition
};
// Dynamic sequence table
// Note: Vector is not a specific class, but a template for the compiler to generate a specific class according to the instantiated type
template<class T>
class Vector
{
public :
Vector(size_t capacity = 10)
: _pData(new T[capacity])
, _size(0)
, _capacity(capacity)
{}
// Demonstration using destructors: declared in a class and defined outside the class.
~Vector();
void PushBack(const T& data);
void PopBack();
// ...
size_t Size() {return _size;}
T& operator[](size_t pos)
{
assert(pos < _size);
return _pData[pos];
}
private:
T* _pData;
size_t _size;
size_t _capacity;
};
// Note: when the functions in the class template are defined outside the class, the template parameter list needs to be added
template <class T>
Vector<T>::~Vector()
{
if(_pData)
delete[] _pData;
_size = _capacity = 0;
}

3.2 instantiation of class template

Class template instantiation is different from function template instantiation. Class template instantiation needs to follow the class template name with < >, and then put the instantiated type in < >. The class template name is not a real class, but the instantiation result is a real class.

// Vector class name, vector < int > is the type
Vector<int> s1;
Vector<double> s2;

4 zero initialization (C++Templates-p71)

For a thousand class template, after instantiating the template with a certain type, in order to confirm that all its members have been initialized. You need to define a default constructor to initialize the members of the class template through an initialization list:

#include<iostream>
using namespace std;


template<typename Type>
class BinTreeNode
{
public:
	BInTreeNode(Type data =Type(),BinTreeNode<Type> *left = nullptr,BinTreeNode<Type>*right = nullptr):
		m_data(data),leftChild(left),rightChild(right)
	{

	}
private:

	Type m_data;
	BinTreeNode* leftChild;
	BinTreeNode* rightChild;

};

5. Non type template parameters (C++Templates-p53)

#include<iostream>
using namespace std;


template<typename Type,size_t _N>//size_t _N is the non type template parameter

class Test
{
private:
	Type ar[_N];
};


void main()
{
	Test<int,10> t;
}

Tags: C++

Posted on Tue, 30 Nov 2021 18:11:17 -0500 by Eddie Fisher