English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++Template Function Detailed Explanation

C++The templates in it can be generally divided into two categories: template functions, template classes. This article first writes template functions, and will introduce template classes next.

Definition: Template functions are generic functions, that is, they use generic types to define, and the generic types can be replaced with specific types.

Code example:

#include <iostream>
//Declaration of template class
template<typename T>
void Swap(T& a,T& b);
int main()
{
  int i = 10;
  int j = 20;
  std::cout << "i=" << i << "," << "j=" << j;
  Swap(i,j);//Generate void Swap(int &,int&);
  std::cout << "i=" << i << "," << "j=" << j;
  double x = 11.5;
  double y = 19.5;
  std::cout << "x=" << x << "," << "y=" << y;
  Swap(x, y);//The compiler generates void Swap(double &, double&);
  std::cout << "x=" << x << "," << "y=" << y;
  return 0;
}
//Definition of template class
template<typename T>
void Swap(T& a, T& b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}

The above example is the simplest function template example, and the compiler will generate the corresponding function according to the specific type used.

Overloaded template:

When the same algorithm needs to be used for multiple different types, templates can be used, as shown in the above code. However, not all types use the same algorithm. To meet this need, template definitions can be overloaded like overloaded conventional function definitions. Like overloaded functions, the feature table of overloaded functions must be different. The code example is as follows:

#include <iostream>
//Declaration of template class
template<typename T>
void Swap(T& a,T& b);
const int iCount = 5;
template<typename T>
void Swap(T* a, T*b, int n);
int main()
{
  int i = 10;
  int j = 20;
  std::cout << "i=" << i << "," << "j=" << j;
  Swap(i,j);//Generate void Swap(int &,int&)
  std::cout << "i=" << i << "," << "j=" << j;
  double x = 11.5;
  double y = 19.5;
  std::cout << "x=" << x << "," << "y=" << y;
  Swap(x, y);//The compiler generates void Swap(double &, double&);
  std::cout << "x=" << x << "," << "y=" << y;
  int d[iCount] = {0,1,2,3,4};
  int e[iCount] = {5,6,7,8,9};
  Swap(d, e, iCount);//Match the new template and perform array exchange
  return 0;
}
//Definition of template class
template<typename T>
void Swap(T& a, T& b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}
template<typename T>
void Swap(T* a, T*b, int n)
{
  for (int i=0; i<iCount;++i)
  {
    T temp;
    temp = a[i];
    a[i] = b[i];
    b[i] = temp;
  }
}

As shown in the code, a new template has been added to exchange elements between two arrays, the original template feature is marked as (T&, T&), and the new template feature is marked as (T[], T[]), int). Note that in the latter template, the type of the last parameter is a specific type (int), not a generic type, and not all template parameters must be template parameter types.

Explicit specialization:

For a given function name, there can be non-template functions, template functions, explicit specialization template functions, and their overloaded versions.

The prototype and definition of explicit specialization should be prefixed with template<> and indicated by the name to specify the type.

Specialization will override the normal template, and non-template functions will override both specialization and normal template.

The following is a non-template function used to exchange the Job structure, a template function, and a specialized prototype.

void Swap(job &, job&);//Non-template function
template <typename T>
void Swap(T&, T&);//Template function
template <> void Swap<job>(job&, job&);//The explicit specialization function, where the job parameter after Swap can be omitted, the function signature is template <> void Swap(job&, job&);

It is pointed out earlier that, when there are multiple prototypes, the compiler will prioritize non-template over explicit specialization and template version in choosing prototypes, and explicit specialization will take precedence over the version generated by using the template.

As shown in the following call:

double u, v;
Swap(u, v);//Use the generic template
job a, b;
swap (a, b)//Use the explicit concretization version.

Instantiation and Concretization:

    To further understand templates, it is necessary to understand the terms instantiation and concretization. Remember, including the function template itself in the code does not generate a function definition, it is just a plan for generating function definitions. When the compiler generates a definition for a specific type using a template, it gets a template instantiation. For example: the function call Swap(i, j) makes the compiler generate an instance of Swap(), which uses int type. The template is not a function definition, but the template instance using int is a function definition. This instantiation method is called implicit instantiation because the compiler knows that a definition is needed because the int parameter is provided when Swap() is called.

    Now the compiler can also allow display instantiation, which means that you can directly command the compiler to generate a specific instance, such as Swap<int>. The syntax is to declare the selected type-Indicate the type with <> symbols and add the keyword template: before the declaration.

template void Swap<int>(int, int);//Display Instantiation

Compilers that implement this feature will generate an instance of Swap() with int type when they see the above declaration.

Unlike display instantiation, display concretization uses one of the following equivalent declarations:

template <> void Swap<int>(int, int);
template <> void Swap(int, int);

The difference lies in that the meaning of these declarations is 'do not use the Swap() template to generate function definitions, but should use independent, specialized function definitions to generate function definitions displayed as int type.'

Note: Trying to use the same type of display concretization and display instantiation in a programming unit will result in an error.

The following is what the editor introduces to everyone about C++Detailed explanation of template functions, hoping it will be helpful to everyone. If you have any questions, please leave me a message.

You May Also Like