Template specialization and template separation operations

1. Template specialization.

(1). Specialization of function templates:

Specialization steps for function templates:

  1. You must first have a basic function template
  2. The keyword template is followed by a pair of empty angle brackets <>
  3. The function name is followed by a pair of angle brackets, which specify the type to be specialized.
  4. Function parameter list: It must be exactly the same as the basic parameter type of the template function. If it is different, the compiler may report an error.
#include<iostream>
#include<sring.h>
using namespace std;

template<class T>// 函数模板
bool IsEqual(T& left, T& right)
{
    
    
	return left == right;
}

template<>  // 模板特化
bool IsEqual<char*>(char*& left, char*& right)
{
    
    
	if (strcmp(left, right) > 0)
		return true;
	return false;
}
int main()
{
    
    
	char* p1 = "hello";
	char* p2 = "world";
	if (p1 == p2)
		cout << p1 << endl;
	else
		cout << p2 << endl;
	return 0;
}
(2). Specialization of class templates:
1. Total specialization:
  • Full specialization is to determine all the parameters in the template parameter class table.
    For example:
template < class T1, class T2>//类模板
class Date
{
    
    
public:
	Date()
	{
    
    
		cout << "Date::Date()" << endl;
	}
private:
	T1 _a;
	T2 _b;
};

template<> //全特化
class Date<int , char>
{
    
    
public:
	Date()
	{
    
    
		cout << "Date<int,char>::Date()" << endl;
	}
private:
	int _a;
	char _b;
};
2. Partial specialization:
  • Any specialized version that further restricts the design of template parameters
template<class T1, class T2>
class Data
{
    
    
public:
	Data() {
    
     cout << "Data<T1, T2>" << endl; }
private:
	T1 _d1;
	T2 _d2;
};

template <class T1>
class Data<T1, int>
{
    
    
public:
	Data() {
    
     cout << "Data<T1, int>" << endl; }
private:
	T1 _d1;
	int _d2;
};

template <typename T1, typename T2>
class Data <T1*, T2*>
{
    
    
public:
	Data() {
    
     cout << "Data<T1*, T2*>" << endl; }
private:
	T1 _d1;
	T2 _d2;
};

int main()
{
    
    
	Data<int, int> d1;
	Data<int, double> d2;
	Data<int*, char*> d3;
	return 0;
}

2. Separate compilation of templates: (not supported by C++ compiler)

1. Separate compilation: A program (project) is implemented by several source files, and each source file is compiled separately to generate a target file. Finally, the process of linking all target files to form a single executable file is called separate compilation mode.
2. If you define a template function in a .h file and compile it in .cpp, an error will occur.
3. Analysis of error causes :
①C/C++ needs to go through four steps of preprocessing -> compilation -> assembly -> linking when the program is running.
②The compiler did not find the instantiation of the function template in the .cpp file, so it was unable to implement the specific content of the function and caused an error.
4. Solution:
① It is also possible to put the declaration and definition in a file "xxx.hpp" or "xxx.h".
②Explicit instantiation at the location defined by the template (explicit instantiation).

3. Template advantages and disadvantages:

  • 【advantage】
  1. Templates reuse code, save resources, and enable faster iterative development.
  2. Enhanced code flexibility
  • 【defect】
  1. Templates can lead to code bloat and longer compilation times
  2. When a template compilation error occurs, the error message is very messy and it is difficult to locate the error.

Guess you like

Origin blog.csdn.net/weixin_42357849/article/details/107776288