{"id":5643,"date":"2019-02-27T19:21:38","date_gmt":"2019-02-27T19:21:38","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/types-non-types-and-templates-as-template-parameters\/"},"modified":"2019-02-27T19:21:38","modified_gmt":"2019-02-27T19:21:38","slug":"types-non-types-and-templates-as-template-parameters","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/types-non-types-and-templates-as-template-parameters\/","title":{"rendered":"Types-, Non-Types, and Templates as Template Parameters"},"content":{"rendered":"<p>I assume you saw the additional keywords <code>typename<\/code> or template used before a name in a template. Me too. Honestly, I was pretty surprised. Today&#8217;s post is about dependent names and various template parameters.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5641\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/optical-illusion-311130_1280.png\" alt=\"optical illusion 311130 1280\" width=\"500\" height=\"438\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/optical-illusion-311130_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/optical-illusion-311130_1280-300x263.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/optical-illusion-311130_1280-1024x896.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/optical-illusion-311130_1280-768x672.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Before I write about dependent names, I should write about template parameters.<\/p>\n<\/p>\n<h2>Template Parameter<\/h2>\n<p>Template parameters can be types, non-types, and templates.<\/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<\/li>\n<li>integral type<\/li>\n<\/ul>\n<p>Integrals 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 can be template parameters. In this case, they are called template parameters. The container adaptors <span style=\"font-family: courier new, courier;\">std::stack<\/span>, <span style=\"font-family: courier new, courier;\">std::queue<\/span>, and<span style=\"font-family: courier new, courier;\"> std::priority_queue<\/span> use, per default, a <span style=\"font-family: courier new, courier;\">std::deque<\/span> to hold their arguments, but you can use a different container. Their usage is straightforward.<\/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%;\">std<span style=\"color: #555555;\">::<\/span>stack<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> stack1;\r\nstack1.push(<span style=\"color: #ff6600;\">5<\/span>);\r\n    \r\nstd<span style=\"color: #555555;\">::<\/span>stack<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;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span> stack2;\r\nstack2.push(<span style=\"color: #ff6600;\">10.5<\/span>);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Their definition may look a little bit weird.<\/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%;\"><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> std<span style=\"color: #555555;\">::<\/span>endl;\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> std<span style=\"color: #555555;\">::<\/span>endl;\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> std<span style=\"color: #555555;\">::<\/span>endl;\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> std<span style=\"color: #555555;\">::<\/span>endl;\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> std<span style=\"color: #555555;\">::<\/span>endl;\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                                                                    <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> std<span style=\"color: #555555;\">::<\/span>endl;\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> std<span style=\"color: #555555;\">::<\/span>endl;\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}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Matrix 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::lis<\/span>t (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=\"400\" height=\"241\" 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: 400px) 100vw, 400px\" \/><\/p>\n<p>But hold, I forget to mention lines 1 and line 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>Preparation done. Now, I can write about dependent names.<\/p>\n<h2>Dependent Names<\/h2>\n<p>First of all. What is a dependent name? A dependent name is essentially a name that depends on a template parameter. Let me show what that means. Here are a few examples based on <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/dependent_name\">cppreference.com<\/a>:<\/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%;\"><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>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> X <span style=\"color: #555555;\">:<\/span> B<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #0099ff; font-style: italic;\">\/\/ \"B&lt;T&gt;\" is dependent on T<\/span>\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">::<\/span>A<span style=\"color: #555555;\">*<\/span> pa; <span style=\"color: #0099ff; font-style: italic;\">\/\/ \"T::A\" is dependent on T<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">f<\/span>(B<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;*<\/span> pb) {\r\n        <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> B<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>i; <span style=\"color: #0099ff; font-style: italic;\">\/\/ \"B&lt;T&gt;::i\" is dependent on T<\/span>\r\n        pb<span style=\"color: #555555;\">-&gt;<\/span>j<span style=\"color: #555555;\">++<\/span>; <span style=\"color: #0099ff; font-style: italic;\">\/\/ \"pb-&gt;j\" is dependent on T<\/span>\r\n    }\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, the fun starts. A dependent name can be a type, a non-type, or a template parameter. The name lookup is the first difference between non-dependent and dependent names.<\/p>\n<ul>\n<li><strong>Non-dependent names<\/strong> are looked up at the point of the template definition.<\/li>\n<li><strong>Dependent names<\/strong> are looked up when the template arguments are known. This means at the point of template instantiation.<\/li>\n<\/ul>\n<p>If you use a dependent name in a template declaration or template definition, the compiler has no idea whether this name refers to a type, a non-type, or a template parameter. In this case, the compiler assumes that the dependent name refers to a non-type, which may be wrong. This is the case in which you have to help the compiler.<\/p>\n<p>Before I show you two examples, I must add an exception to this rule. You can skip my following few words if you want to get a general idea and jump directly to the following subsection. The exception is: if the name refers in the template definition to the current instantiation, the compiler can deduce the name at the point of the template definition. Here are a few examples:<\/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%;\"><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> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">A<\/span> {\r\n    A<span style=\"color: #555555;\">*<\/span> p1;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ A is the current instantiation<\/span>\r\n    A<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;*<\/span> p2; <span style=\"color: #0099ff; font-style: italic;\">\/\/ A&lt;T&gt; is the current instantiation<\/span>\r\n    <span style=\"color: #555555;\">::<\/span>A<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;*<\/span> p4; <span style=\"color: #0099ff; font-style: italic;\">\/\/ ::A&lt;T&gt; is the current instantiation<\/span>\r\n    A<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">*&gt;<\/span> p3; <span style=\"color: #0099ff; font-style: italic;\">\/\/ A&lt;T*&gt; is not the current instantiation<\/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;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">A<\/span><span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">*&gt;<\/span> {\r\n    A<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">*&gt;*<\/span> p1;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ A&lt;T*&gt; is the current instantiation<\/span>\r\n    A<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;*<\/span> p2;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ A&lt;T&gt; is not the current instantiation<\/span>\r\n};\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> I<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">struct<\/span> B {\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> my_I <span style=\"color: #555555;\">=<\/span> I;\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> my_I2 <span style=\"color: #555555;\">=<\/span> I<span style=\"color: #555555;\">+<\/span><span style=\"color: #ff6600;\">0<\/span>;\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> my_I3 <span style=\"color: #555555;\">=<\/span> my_I;\r\n    B<span style=\"color: #555555;\">&lt;<\/span>my_I<span style=\"color: #555555;\">&gt;*<\/span> b3;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ B&lt;my_I&gt; is the current instantiation<\/span>\r\n    B<span style=\"color: #555555;\">&lt;<\/span>my_I2<span style=\"color: #555555;\">&gt;*<\/span> b4; <span style=\"color: #0099ff; font-style: italic;\">\/\/ B&lt;my_I2&gt; is not the current instantiation<\/span>\r\n    B<span style=\"color: #555555;\">&lt;<\/span>my_I3<span style=\"color: #555555;\">&gt;*<\/span> b5; <span style=\"color: #0099ff; font-style: italic;\">\/\/ B&lt;my_I3&gt; is the current instantiation<\/span>\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Finally, I came to the critical idea of my post. If a dependent name could be a type, a non-type, or a template, you have to give the compiler a hint.<\/p>\n<h3>Use <span style=\"font-family: courier new, courier;\">typename<\/span> if the Dependent Name is a Type<\/h3>\n<p>After such a long introduction, the following program snippet makes it pretty clear.<\/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%;\"><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>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> test(){\r\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>const_iterator<span style=\"color: #555555;\">*<\/span> p1;          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>const_iterator<span style=\"color: #555555;\">*<\/span> p2; <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Without the <span style=\"font-family: courier new, courier;\">typename<\/span> keyword in line 2, the name<span style=\"font-family: courier new, courier;\"> std::vector&lt;T&gt;::const_iterator<\/span> in line 2 would be interpreted as a non-type and, consequently, the * stands for multiplication and not for a pointer declaration. Exactly this is happening in line (1).<\/p>\n<p>Similarly, if your dependent name should be a template, you have to give the compiler a hint.<\/p>\n<h3>Use <span style=\"font-family: courier new, courier;\">.template<\/span> if the Dependent Name is a Template<\/h3>\n<p>Honestly, this syntax looks a little bit weird.<\/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%;\"><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>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> S{\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> U<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> func(){}\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>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> func2(){\r\n    S<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> s;\r\n    s.func<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>();             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    s.<span style=\"color: #006699; font-weight: bold;\">template<\/span> func<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>();    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Same story as before. Compare lines 1 and 2. When the compiler reads the name <span style=\"font-family: courier new, courier;\">s.func<\/span>&nbsp;(line 1), it interprets it as non-type. This means the <span style=\"font-family: courier new, courier;\">&lt;<\/span> sign stands for the comparison operator but not opening the square bracket of the template argument of the generic method <span style=\"font-family: courier new, courier;\">func<\/span>. In this case, you must specify that <span style=\"font-family: courier new, courier;\">s.func<\/span> is a template, such as in line 2:<span style=\"font-family: courier new, courier;\"> s.template func<\/span>.&nbsp;<\/p>\n<p>Here is the essence of this post in one sentence:<strong> When you have a dependent name, use <span style=\"font-family: courier new, courier;\">typename<\/span> to specify that it is a type or <span style=\"font-family: courier new, courier;\">.template<\/span> to specify that it is a template.<\/strong><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The following rules in the C++ Core Guidelines are C-style programming and source files. Let&#8217;s see in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-other-template-rules\">next post<\/a> what this means.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I assume you saw the additional keywords typename or template used before a name in a template. Me too. Honestly, I was pretty surprised. Today&#8217;s post is about dependent names and various template parameters.<\/p>\n","protected":false},"author":21,"featured_media":5641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5643","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=5643"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5643\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5641"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}