{"id":6232,"date":"2021-10-07T06:22:16","date_gmt":"2021-10-07T06:22:16","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/dependent-types\/"},"modified":"2024-07-22T16:00:39","modified_gmt":"2024-07-22T16:00:39","slug":"dependent-types","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/dependent-types\/","title":{"rendered":"Dependent Names"},"content":{"rendered":"<p>A dependent name is essentially a name that depends on a template parameter. A dependent name can be a type, a non-type, or a template parameter. To express that a dependent name stands for a type or a template, you have to use the keywords <code>typename<\/code> or <code>template<\/code>.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6231\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/templates.png\" alt=\"templates\" width=\"650\" height=\"396\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/templates.png 1929w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/templates-300x183.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/templates-1024x624.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/templates-768x468.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/10\/templates-1536x936.png 1536w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Before I write about dependent names, I should write about template parameters.<\/p>\n<h2>Template Parameter<\/h2>\n<p>Template parameters can be types, non-types, and templates.<\/p>\n<h3>Types<\/h3>\n<p>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;\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;\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;\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 types<\/li>\n<li>floating-point types (C++20)<\/li>\n<li>literal types (C++20)<\/li>\n<\/ul>\n<p>Non-type template parameters are often just called NTTP.<\/p>\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 \/>\n<\/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>};\n<\/pre>\n<\/div>\n<p>With C++20, you can also use two new non-types: floating-point types and literal types.<\/p>\n<p>Literal Types must have the following two properties, essentially:<\/p>\n<ul>\n<li>All base classes and non-static data members are public and non-mutable<\/li>\n<li>The types of all base classes and non-static data members are structural types or arrays of these<\/li>\n<\/ul>\n<p>A literal type must have a <code>constexpr<\/code> constructor.<\/p>\n<p>The following program uses floating-point types and literal types as template parameters.<\/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: #0099ff; font-style: italic;\">\/\/ nonTypeTemplateParameter.cpp<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> ClassType {\n    constexpr ClassType(<span style=\"color: #007788; font-weight: bold;\">int<\/span>) {}  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n};\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span>ClassType cl<span style=\"color: #555555;\">&gt;<\/span>          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> getClassType() {\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> cl;\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span> d<span style=\"color: #555555;\">&gt;<\/span>              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> getDouble() {\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> d;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> c1 <span style=\"color: #555555;\">=<\/span> getClassType<span style=\"color: #555555;\">&lt;<\/span>ClassType(<span style=\"color: #ff6600;\">2020<\/span>)<span style=\"color: #555555;\">&gt;<\/span>();\n\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> d1 <span style=\"color: #555555;\">=<\/span> getDouble<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">5.5<\/span><span style=\"color: #555555;\">&gt;<\/span>();  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> d2 <span style=\"color: #555555;\">=<\/span> getDouble<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">6.5<\/span><span style=\"color: #555555;\">&gt;<\/span>();  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n\n}\n<\/pre>\n<\/div>\n<p>ClassType has a <code>constexpr<\/code> constructor (1) and can be used as a template argument (2). The function template\u00a0<code>getDouble<\/code> (3) accepts only doubles. I want to emphasize it explicitly that each call of the function template\u00a0<code>getDouble<\/code> (4) with a new argument triggers the instantiation of a new function template specialization of <code>getDouble<\/code>.\u00a0 This means that two instantiations for the doubles 5.5 and 6.5 are created.<\/p>\n<h3>Templates<\/h3>\n<p>Templates can be template parameters; in this case, they are called template template parameters.<\/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>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;list&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n\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>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Matrix<\/span>{\n<span style=\"color: #9999ff;\">public:<\/span>\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>\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>;\n  }\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> getSize() <span style=\"color: #006699; font-weight: bold;\">const<\/span>{\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> data.size();\n  }\n\n<span style=\"color: #9999ff;\">private:<\/span>\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>\n\n};\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\n\n                                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\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>}; \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\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';\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\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>\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\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';\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n                                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\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>};  \n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\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';\n\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\n\n}\n<\/pre>\n<\/div>\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::list<\/span> (line 6) to hold its values. So far, nothing special.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5642\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateTemplateParameters.png\" alt=\"templateTemplateParameters\" width=\"400\" height=\"241\" 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>Line 1 declares a class template that has two template parameters. 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. 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>\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span>,\n    <span style=\"color: #006699; font-weight: bold;\">typename<\/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>\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>;\n<\/pre>\n<\/div>\n<p>Line 3 shows the allocator used in this internally used container. The matrix can be instantiated with all containers 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>.\u00a0 <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>Now, I can write about dependent names.<\/p>\n<h2>Dependent Names<\/h2>\n<p>What is a dependent name? A dependent name is essentially a name that depends on a template parameter. Here are a few examples based on <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/dependent_name\">cppreference.com<\/a>:<\/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>\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>\n{\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>\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) {\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>\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>\n    }\n};\n<\/pre>\n<\/div>\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 big 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 at the point of the template instantiation.<\/li>\n<\/ul>\n<p>When you use a dependent name in a template declaration, 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 when you have to give the compiler a hint.<\/p>\n<p>Before I show you two examples, I must write about an exception to this rule. You can skip my next few words if you want to get a general idea and jump directly to the next section. The exception is if the name refers to the current instantiation in the template definition; the compiler can deduce the name at the point of the template definition. 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%;\"><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: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span> \n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">A<\/span> {\n    A<span style=\"color: #555555;\">*<\/span> p1;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ A is the current instantiation<\/span>\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>\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>\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>\n};\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> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span><span style=\"color: #555555;\">&gt;<\/span> \n<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> {\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>\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>\n};\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> \n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> B {\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;\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>;\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;\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>\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>\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>\n};\n<\/pre>\n<\/div>\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 must 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 clarifies it.<\/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>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> test(){\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>\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>\n}\n<\/pre>\n<\/div>\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. This is exactly what happens in line (1).<\/p>\n<p>Similarly, if your dependent name should be a template, you must 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 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: #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>\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> S {\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(){}\n}\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>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> func2(){\n    S<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> s;\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>\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>\n}\n<\/pre>\n<\/div>\n<p>It&#8217;s the 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>\u00a0(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>.<\/p>\n<p>Here is the essence of this post in one sentence:<strong> When you have a dependent name, use <code><span style=\"font-family: courier new, courier;\">typename<\/span><\/code> to specify that the dependent name is a type or use <code><span style=\"font-family: courier new, courier;\">.template<\/span><\/code> to specify that the dependent name is a template.<\/strong><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In my next post, I will present automatic return types. They are often a lifesaver when it comes to function templates.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A dependent name is essentially a name that depends on a template parameter. A dependent name can be a type, a non-type, or a template parameter. To express that a dependent name stands for a type or a template, you have to use the keywords typename or template.<\/p>\n","protected":false},"author":21,"featured_media":6231,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[438],"class_list":["post-6232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-dependent-names"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6232","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=6232"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6232\/revisions"}],"predecessor-version":[{"id":9763,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6232\/revisions\/9763"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6231"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}