Introduction to the Template Specialization | heise developer

Share your love

Templates define the behavior of families of classes or functions. It is often necessary to give special treatment to specific types or non-types. To meet this use case, you can specialize in templates.

Template specialization

First, I’d like to shed some light on the general idea behind template specialization. In the next article, I’ll focus on the details.

Template specialization

Templates define the behavior of families of classes and functions. It is often necessary to give special treatment to specific types or non-types. To do this, you can completely specialize templates.

Class templates can also be partially specialized. The general or primary template can coexist with partially or fully specialized templates. The member functions and attributes of a specialization do not have to be identical to those of the primary template. The compiler prefers fully specialized over partially specialized templates, and partially specialized templates over primary templates.

The following example should clarify my words:

template <typename T, int Line, int Column> // (1)
class Matrix;

template <typename T> // (2)
class Matrix<T, 3, 3>{};

template <> // (3)
class Matrix<int, 3, 3>{};

Line 1 describes the primary or general template. The primary template must be declared before the partially or fully specialized templates. If the primary template is not required, a declaration as in line 1 is OK.

  • Partial specialization

Partial specialization follows in line 2. Only class templates support partial specialization. A partial specialization has template parameters and explicitly specified template arguments. In the specific case of the class Matrix<T, 3, 3> is T the template parameter and the numbers are the template arguments.

  • Complete specialization

Line 3 shows a full specialization. Complete means that all template arguments are specified and the template parameter list is empty: template <> in line 3.

Partial versus full specialization

To better understand partial and full specialization, I want to present a visual explanation. You may know that I studied mathematics and had many systems of linear equations to solve.

Think of an n-dimensional space of template parameters. Partial specialization is a subspace in n-dimensional space, and full specialization is a point in n-dimensional space.

Now I’ll apply my visual explanation to the class template Matrix and their partial and full specialization. In the primary template (line 1) you can choose one type as template parameter and two int values ​​as non-type template parameters. With the partial specialization in line 2, only the type can be selected. This means that the 3-dimensional space is reduced to a line. The partial specialization of the primary template Matrix is thus a subspace of the 3-dimensional space. The full specialization (line 3) stands for a point in 3-dimensional space.

What happens when you use the template?

Using primary, partial, and full specialization

As a reminder, the following specializations of the Matrix class are given:

template <typename T, int Line, int Column> // (1)
class Matrix;

template <typename T> // (2)
class Matrix<T, 3, 3>{};

template <> // (3)
class Matrix<int, 3, 3>{};

The question is, what if you instantiate Matrix for different template arguments? The following example shows three instantiations and their implementation by the compiler:

Matrix<int, 3, 3> m1; // class Matrix<int, 3, 3>

Matrix<double, 3, 3> m2; // class Matrix<T, 3, 3>

Matrix<std::string, 4, 3> m3; // class Matrix<T, Line, Column> => ERROR

m1 uses full specialization, m2 the partial specialization and m3 the primary template. In the absence of definition, the use of m3 an error.

To understand this process, you need to keep a few rules in mind. The following rules apply in particular to the partial specialization of class templates:

  • Dependencies between the template parameter and the template arguments
    • The number and order of the explicitly specified template arguments (<T, 3, 3>) must match the number and order of the template parameter list (<typename T, int Line, int Column>) of the primary template match.
    • If you are using default values ​​for template parameters, you do not need to specify the template arguments. Only the primary template accepts default values ​​for template parameters.
  • Valid partial specializations
    • The compiler chooses a partial specialization if the arguments of the template instantiation (Matrix<double, 3, 3>) a subset of the template arguments of the partial specialization (Matrix<T, 3, 3>) are.
  • Template specialization used
    • The compiler only finds one specialization. He uses this specialization.
    • The compiler finds more than one specialization. He uses the most specialized. If this process ends in more than one specialization, the compiler throws an error.
    • The compiler does not find any specialization. He uses the primary specialization.

Okay, I have one more question to answer. What does it mean that a template A is a more specialized template than a specialized template B. My informal definition is as follows:

A template A is more specialized than a template B:

  • Template B can accept all arguments that Template A can accept.
  • Template B can accept arguments that Template A cannot accept.

If you want it to be more formal, visit and read the subsection on partial ordering.

What’s next?

This article should give you the basics of template specialization, but as always, there are more details about it in C ++. For example, partial or full specialization behaves like a if at compile time and full specialization of class or function templates are very similar to normal classes or functions.

C ++ Sow

In the next six months I will be offering the following seminars. If the Covid-19 situation allows, I will hold the seminars as face-to-face seminars after consultation.

Article Source

Read Also   The Samsung Galaxy FE S21 will not arrive this year according to a report
Share your love