{"id":6151,"date":"2021-06-04T07:06:51","date_gmt":"2021-06-04T07:06:51","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/alias-templates-and-template-parameters\/"},"modified":"2021-06-04T07:06:51","modified_gmt":"2021-06-04T07:06:51","slug":"alias-templates-and-template-parameters","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/alias-templates-and-template-parameters\/","title":{"rendered":"Alias Templates and Template Parameters"},"content":{"rendered":"<p>Today, I write about two topics: alias templates and template parameters. Alias templates are a way to give a name to a family of types. Template parameters can be types, non-types, and templates themselves.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6149\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesParameters.png\" alt=\"templatesParameters\" width=\"650\" height=\"394\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesParameters.png 926w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesParameters-300x182.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/templatesParameters-768x466.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Let&#8217;s start with the alias templates.<\/p>\n<h2>Alias Templates<\/h2>\n<p>With C++11, we got alias templates. Alias templates provide a means to give a convenient name to a family of types. The following code snippet presents the idea for the class template Matrix.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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> Col<span style=\"color: #555555;\">&gt;<\/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};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><code>Matrix<\/code> has three template parameters. The type parameter, and the non-type parameters, and <code>Col<\/code> (I will write about template parameters in the next section.)<\/p>\n<p>For readability, I want to have two special matrices: a <code>Square<\/code> and a <code>Vector<\/code>. A <code>Square<\/code>&#8216;s number of lines and columns should be equal. A <code>Vector<\/code>&#8216;s line size should be one. Thanks to type aliases, I can express my ideas directly in code.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> Square <span style=\"color: #555555;\">=<\/span> Matrix<span style=\"color: #555555;\">&lt;<\/span>T, Line, Line<span style=\"color: #555555;\">&gt;<\/span>; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/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: #007788; font-weight: bold;\">int<\/span> Line<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> Vector <span style=\"color: #555555;\">=<\/span> Matrix<span style=\"color: #555555;\">&lt;<\/span>T, Line, <span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The keyword <code>using <\/code>((1) and (2)) declares a type alias. While the primary template <code>Matrix<\/code> can be parametrized in the three dimensions <code>T<\/code>, <code>Line<\/code>, and <code>Col<\/code>, the type aliases <code>Square<\/code> and <code>Vector <\/code>reduce the parametrization to the two dimensions<code> T<\/code> and <code>Line<\/code>. From this point of view, alias templates enable it to create intuitive names for partially bound templates. Using&nbsp;<code>Square<\/code> and <code>Vector<\/code> is straightforward.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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;\">5<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> ma;\r\nSquare<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #ff6600;\">4<\/span><span style=\"color: #555555;\">&gt;<\/span> sq;\r\nVector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char<\/span>, <span style=\"color: #ff6600;\">5<\/span><span style=\"color: #555555;\">&gt;<\/span> vec;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>An excellent use case of alias templates is the<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\"> type-traits library<\/a>.<\/p>\n<h3>Type-Traits Library<\/h3>\n<p>When you apply <code>std::move(arg)<\/code> on a value <code>arg<\/code>, the compiler uses typically <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/remove_reference\"><code>std::remove_reference<\/code><\/a> to remove a reference from the underlying type:<\/p>\n<p><!-- HTML generated using hilite.me --><\/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;\">static_cast<\/span><span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>remove_reference<span style=\"color: #555555;\">&lt;<\/span>decltype(arg)<span style=\"color: #555555;\">&gt;::<\/span>type<span style=\"color: #555555;\">&amp;&amp;&gt;<\/span>(arg);   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">static_cast<\/span><span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">remove_reference_t<\/span><span style=\"color: #555555;\">&lt;<\/span>decltype(arg)<span style=\"color: #555555;\">&gt;&amp;&amp;&gt;<\/span>(arg);       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Thanks to alias templates, version (line 2) have been valid since C++14. The following helper type is available:<\/p>\n<p><!-- HTML generated using hilite.me --><\/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;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span> <span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #007788; font-weight: bold;\">remove_reference_t<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">typename<\/span> remove_reference<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>type;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Of course, the corresponding helper types for the other functions of the<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\"> type-traits library<\/a> returning a type are also available with C++14.<\/p>\n<p>&nbsp;<\/p>\n<p>The previously defined class template <code>Matrix<\/code> uses the two non-type template parameters <code>Line<\/code> and <code>Col<\/code>.<\/p>\n<\/p>\n<h2>Template Parameters<\/h2>\n<p>&nbsp;Template parameters can be types, non-types, and templates themselves.<\/p>\n<h3>Types<\/h3>\n<p>Okay, types are the most often used template parameters. Here are a few examples:<\/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%;\">std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myVec;\r\nstd<span style=\"color: #555555;\">::<\/span>map<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string, <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myMap;\r\nstd<span style=\"color: #555555;\">::<\/span>lock_guard<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> myLockGuard;\r\n<\/pre>\n<\/div>\n<h3>Non-Types<\/h3>\n<p>Non-types can be a<\/p>\n<ul>\n<li>lvalue reference<\/li>\n<li><span style=\"font-family: 'courier new', courier;\">nullptr<\/span><\/li>\n<li>pointer<\/li>\n<li>enumerator of a <code>enum<\/code><\/li>\n<li>integral values<\/li>\n<li>floating-point values (C++20)<\/li>\n<\/ul>\n<p>Integral values are the most used non-types. <span style=\"font-family: courier new, courier;\">std::array<\/span> is the typical example because you have to specify at compile time the size of a <span style=\"font-family: courier new, courier;\">std::array:<br \/><\/span><\/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%;\">std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> myArray{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};\r\n<\/pre>\n<\/div>\n<h3>Templates<\/h3>\n<p>Templates themself can be template parameters. Their definition may look a bit weird.<\/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: #0099ff; font-style: italic;\">\/\/ templateTemplateParameters.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;list&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/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: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span>, <span style=\"color: #006699; font-weight: bold;\">typename<\/span><span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Cont<\/span> <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<span style=\"color: #9999ff;\">public:<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Matrix(std<span style=\"color: #555555;\">::<\/span>initializer_list<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> inList)<span style=\"color: #555555;\">:<\/span> data(inList) {  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> d<span style=\"color: #555555;\">:<\/span> data) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> d <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n  }\r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> getSize() <span style=\"color: #006699; font-weight: bold;\">const<\/span>{\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> data.size();\r\n  }\r\n\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n  Cont<span style=\"color: #555555;\">&lt;<\/span>T, std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&gt;<\/span> data;                                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)                               <\/span>\r\n\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\r\n\r\n                                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n  Matrix<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&gt;<\/span> myIntVec{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>, <span style=\"color: #ff6600;\">10<\/span>}; \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt; <\/span>'\\n';\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myIntVec.getSize(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myIntVec.getSize() <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n'<span style=\"color: #555555;\"><\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n  Matrix<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&gt;<\/span> myDoubleVec{<span style=\"color: #ff6600;\">1.1<\/span>, <span style=\"color: #ff6600;\">2.2<\/span>, <span style=\"color: #ff6600;\">3.3<\/span>, <span style=\"color: #ff6600;\">4.4<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>}; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myDoubleVec.getSize(): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> myDoubleVec.getSize() <span style=\"color: #555555;\">&lt;&lt; <\/span>'\\n';\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\r\n                                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n  Matrix<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string, std<span style=\"color: #555555;\">::<\/span>list<span style=\"color: #555555;\">&gt;<\/span> myStringList{<span style=\"color: #cc3300;\">\"one\"<\/span>, <span style=\"color: #cc3300;\">\"two\"<\/span>, <span style=\"color: #cc3300;\">\"three\"<\/span>, <span style=\"color: #cc3300;\">\"four\"<\/span>};  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"myStringList.getSize(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myStringList.getSize() <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><code>Matrix<\/code> is a simple class template that can be initialized by a<span style=\"font-family: courier new, courier;\"> std::initializer_list<\/span> (line 2). A <span style=\"font-family: courier new, courier;\">Matrix<\/span> can be used with a <span style=\"font-family: courier new, courier;\">std::vector<\/span> (line 4 and line 5) or a<span style=\"font-family: courier new, courier;\"> std::list<\/span> (line 6) to hold its values. So far, nothing special.&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5642\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateTemplateParameters.png\" alt=\"templateTemplateParameters\" width=\"450\" height=\"271\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateTemplateParameters.png 785w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateTemplateParameters-300x181.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateTemplateParameters-768x463.png 768w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>But hold, I forget to mention lines 1 and 3. Line 1 declares a class template that has two template parameters. Okay, the first parameter is the type of the elements, and the second parameter stands for the container. Look at the second parameter:<span style=\"font-family: courier new, courier;\"> template &lt;typename, typename&gt; class Cont &gt;. <\/span>This means the second template argument should be a template requiring two template parameters. The first template parameter is the type of elements the container stores, and the second is the defaulted allocator a container of the standard template library has. Even the allocator has a default value, such as in the case of a <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/vector\">std::vector<\/a>. The allocator depends on the type of elements.<\/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>\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span>,\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Allocator<\/span> <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">vector<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line 3 shows the usage of the allocator in this internally used container. The matrix can use all containers, which are of the kind: <span style=\"font-family: courier new, courier;\">container&lt; type of the elements, allocator of the elements&gt;<\/span>. This is true for the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\">sequence containers<\/a> such as <span style=\"font-family: courier new, courier;\">std::vector<\/span>, <span style=\"font-family: courier new, courier;\">std::deque<\/span>, or<span style=\"font-family: courier new, courier;\"> std::list<\/span>.&nbsp; <span style=\"font-family: courier new, courier;\">std::array<\/span> and <span style=\"font-family: courier new, courier;\">std::forward_list<\/span> would fail because<span style=\"font-family: courier new, courier;\"> std::array<\/span> needs an additional non-type for specifying its size at compile-time and <span style=\"font-family: courier new, courier;\">std::forward_list<\/span> does not support the <span style=\"font-family: courier new, courier;\">size<\/span> method.<\/p>\n<p>Maybe you don&#8217;t like the keyword class for the name of the template template parameter. With C++17, you can replace <code>class<\/code> with <code>typename<\/code>:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/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: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span>, <span style=\"color: #006699; font-weight: bold;\">typename<\/span><span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Cont<\/span> <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: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span>, <span style=\"color: #006699; font-weight: bold;\">typename<\/span><span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Cont <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>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Line (2) is valid since C++17 and is equivalent to line (1).<\/p>\n<h2>The Next pdf Bundle: Coroutines<\/h2>\n<p>In the post <a href=\"https:\/\/www.modernescpp.com\/index.php\/which-pdf-bundle-do-you-want-make-your-choice\">&#8220;Which pdf bundle do you want? Make your choice!<\/a>&#8221; you decided on the coroutines bundle.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6150\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult.png\" alt=\"pollResult\" width=\"650\" height=\"629\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult.png 893w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult-300x291.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/06\/pollResult-768x744.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I&#8217;m still preparing the bundle, but it should be available in the next few days.<\/p>\n<p>If you subscribe to the English newsletter, you automatically get the link to the current pdf bundle. Have a look at the top right corner of this page. This automatism makes it quite comfortable for me. People that are already subscribed to my newsletter get the link automatically.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In my <a href=\"https:\/\/www.modernescpp.com\/index.php\/template-arguments\">next post<\/a>, I will write about template arguments. It is pretty interesting how the compiler deduces the types. The rules do not only apply to function templates (C++98) but also to&nbsp;<code>auto<\/code> (C++11), to class templates (C++17), and concepts (C++20).<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I write about two topics: alias templates and template parameters. Alias templates are a way to give a name to a family of types. Template parameters can be types, non-types, and templates themselves.<\/p>\n","protected":false},"author":21,"featured_media":6149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[],"class_list":["post-6151","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\/6151","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=6151"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6151\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6149"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}