{"id":6166,"date":"2021-06-24T07:20:10","date_gmt":"2021-06-24T07:20:10","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/template-specialization\/"},"modified":"2021-06-24T07:20:10","modified_gmt":"2021-06-24T07:20:10","slug":"template-specialization","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/template-specialization\/","title":{"rendered":"Template Specialization"},"content":{"rendered":"<p>Templates define the behavior of families of classes or functions. Often it is required that particular types or non-types may be treated special. To support this use case, you can specialize templates.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6165\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization.png\" alt=\"TemplateSpecialization\" width=\"650\" height=\"409\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization.png 926w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization-300x189.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/TemplateSpecialization-768x483.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Let me start this post with the general idea of template specialization. In the next post, I concentrate on the details.<\/p>\n<\/p>\n<h2>Template Specialization<\/h2>\n<p>Templates define the behavior of families of classes and functions. Often it is required that particular types or non-types must be treated special. Therefore, you can fully specialize templates.<\/p>\n<p>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 specialization don\u2019t have to be identical to those of the primary template. The compiler prefers fully specialized to partially specialized templates and partially specialized templates to primary templates.<\/p>\n<p>The following example should clarify my words.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #007788; font-weight: bold;\">int<\/span> Line, <span style=\"color: #007788; font-weight: bold;\">int<\/span> Column<span style=\"color: #555555;\">&gt;<\/span>     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Matrix<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Matrix<\/span><span style=\"color: #555555;\">&lt;<\/span>T, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span>{};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;&gt;<\/span>                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Matrix<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span>{};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>Primary Template<\/li>\n<\/ul>\n<p>Line 1 is the primary or general template. The primary template has to be declared before the partially or fully specialized templates. If the primary template is unnecessary, a declaration such as in line 1 is acceptable.<\/p>\n<ul>\n<li>Partial Specialization<\/li>\n<\/ul>\n<p>Line 2 follows with the partial specialization. Only class templates support partial specialization. A partial specialization has template parameters and explicitly specified template arguments. In the concrete case<code>, class Matrix&lt;T, 3, 3&gt; T<\/code> is the template parameter, and the numbers are the template arguments.<\/p>\n<ul>\n<li>Full Specialization<\/li>\n<\/ul>\n<p>&nbsp;Line 3 is the full specialization. Full means that all template arguments are specified and the template parameter list is empty:<code> template &lt;&gt;<\/code> in line 3.<\/p>\n<h3>Partial versus Full Specialization<\/h3>\n<p>To better understand partial and full specialization, I want to present a visual explanation. You may know I studied mathematics, and I had many linear systems of equations to solve.<\/p>\n<p>Think about an n-dimensional space of template parameters. A partial specialization is a subspace in the n-dimensional space, and a full specialization is a point in the n-dimensional space.<\/p>\n<p>I apply my visual explanation to the class template <code>Matrix<\/code> and its partial and full specialization. In the primary template (line 1), you can choose a type as a template parameter and two <code>int<\/code> values as non-type template parameters. Regarding the partial specialization in line 2, you can only choose the type. This means the 3-dimensional space is reduced to a line. The partial specialization of the primary template<code> Matrix<\/code> is, therefore, a subspace of the 3-dimensional space. The full specialization (line 3) is a point in the 3-dimensional space.&nbsp;<\/p>\n<p>What happens when you invoke the templates?<\/p>\n<h3>Using the Primary, Partial, and Full Specialization<\/h3>\n<p>To remind you, the following specializations of the class <code>Matrix<\/code> are given.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #007788; font-weight: bold;\">int<\/span> Line, <span style=\"color: #007788; font-weight: bold;\">int<\/span> Column<span style=\"color: #555555;\">&gt;<\/span>     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Matrix<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Matrix<\/span><span style=\"color: #555555;\">&lt;<\/span>T, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span>{};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;&gt;<\/span>                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Matrix<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span>{};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The question is: What happens when you instantiate <code>Matrix<\/code> for various template arguments? Here are three instantiations, and you see what the compiler creates.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">Matrix<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> m1;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ class Matrix&lt;int, 3, 3&gt;<\/span>\r\n\r\nMatrix<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> m2;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ class Matrix&lt;T, 3, 3&gt; <\/span>\r\n\r\nMatrix<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> m3;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ class Matrix&lt;T, Line, Column&gt; =&gt; ERROR<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">m1<\/span> uses the entire specialization, <span style=\"font-family: courier new, courier;\">m2<\/span> uses the partial specialization, and <span style=\"font-family: courier new, courier;\">m3<\/span> uses the primary template, which causes an error because the definition is missing.<\/p>\n<p>To understand this process, you must remember a few rules. Here are the rules that apply, in particular, to the partial specialization of class templates.<\/p>\n<h4>Dependencies between the Template Parameter and the Template Arguments<\/h4>\n<ul>\n<li>The number and sequence of the explicitly specified template arguments (<code>&lt;T, 3, 3&gt;<\/code>) must match the number and sequence of the template parameter list (<code>&lt;typename T, int Line, int Column&gt;<\/code>) of the primary template.<\/li>\n<li>If you use defaults for template parameters, you don&#8217;t have to provide the template arguments. Only the primary template accepts defaults for template parameters.<\/li>\n<\/ul>\n<h4>Valid Partial Specializations<\/h4>\n<ul>\n<li>The compiler chooses a partial specialization if the template instantiation arguments <code>(Matrix&lt;double, 3, 3&gt;<\/code>) are a subset of the template arguments of the partial specialization (<code>Matrix&lt;T, 3, 3&gt;<\/code>).<\/li>\n<\/ul>\n<h4>Chosen Template Specialization<\/h4>\n<ol>\n<li>The compiler finds only one specialization. The compiler uses this specialization.<\/li>\n<li>The compiler finds more than one specialization. The compiler uses the most specialized one. If this process ends in more than one specialization, the compiler throws an error.<\/li>\n<li>The compiler finds no specialization. It uses the primary specialization.<\/li>\n<\/ol>\n<p>Okay, there is one question left I have to answer. What does it mean that a template<code> A<\/code> is a more specialized template than another template<code> B<\/code>. This is my informal definition. <em><br \/><\/em><\/p>\n<p><strong>A template A is more specialized than a template B:<\/strong><\/p>\n<ul>\n<li>Template B can accept all arguments that template A can accept.<\/li>\n<li>Template B can accept arguments that template A cannot accept.<\/li>\n<\/ul>\n<p>If you want to have it more formal, visit <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/partial_specialization\">cppreference.com\/partial_specialization<\/a> and go to the subsection about partial ordering.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>This post should provide you with the basics about template specialization, but as always, there are more details to it in C++. For example, partial or full specialization behaves like a compile-time if and full specialization of class or function templates is quite similar to ordinary classes or functions.<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Templates define the behavior of families of classes or functions. Often it is required that particular types or non-types may be treated special. To support this use case, you can specialize templates.<\/p>\n","protected":false},"author":21,"featured_media":6165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[],"class_list":["post-6166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6166"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6166\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6165"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}