{"id":5640,"date":"2019-02-22T09:06:19","date_gmt":"2019-02-22T09:06:19","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/templates-misconceptions-and-surprises\/"},"modified":"2019-02-22T09:06:19","modified_gmt":"2019-02-22T09:06:19","slug":"templates-misconceptions-and-surprises","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/templates-misconceptions-and-surprises\/","title":{"rendered":"Templates: Misconceptions and Surprises"},"content":{"rendered":"<p>I often teach the basics of templates. Templates are special. Therefore, I encounter many misconceptions which cause surprises. Here are a few of them.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5634\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/santa-claus-2927962_1280.png\" alt=\"santa claus 2927962 1280\" width=\"400\" height=\"446\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/santa-claus-2927962_1280.png 1148w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/santa-claus-2927962_1280-269x300.png 269w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/santa-claus-2927962_1280-918x1024.png 918w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/santa-claus-2927962_1280-768x856.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>My first misconception is presumably apparent for many but not all C++ developers.<\/p>\n<h2>Templates of Related Types are not Related<\/h2>\n<p>First of all, what does the related type mean? My informal term stands for types that can be <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/implicit_conversion\">implicitly converted<\/a>. Here is the starting point.<\/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;\">\/\/ genericAssignment.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;vector&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: #007788; font-weight: bold;\">int<\/span> N<span style=\"color: #555555;\">&gt;<\/span>             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Point{\r\n    Point(std<span style=\"color: #555555;\">::<\/span>initializer_list<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> initList)<span style=\"color: #555555;\">:<\/span> coord(initList){}\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> coord;    \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    Point<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> point1{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};\r\n    Point<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> point2{<span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>};\r\n  \r\n    point1 <span style=\"color: #555555;\">=<\/span> point2;                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> doubleValue <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2.2<\/span>;             \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> intValue <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;\r\n    doubleValue <span style=\"color: #555555;\">=<\/span> intValue;             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    \r\n    Point<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> point3{<span style=\"color: #ff6600;\">1.1<\/span>, <span style=\"color: #ff6600;\">2.2<\/span>, <span style=\"color: #ff6600;\">3.3<\/span>};\r\n    point3 <span style=\"color: #555555;\">=<\/span> point2;                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class template <span style=\"font-family: courier new, courier;\">Point<\/span> stands for a point in an n-dimensional space. The type of the coordinates and the dimension can be adjusted (line 1). The coordinates are stored in a <span style=\"font-family: courier new, courier;\">std::vector&lt;T&gt;<\/span>. When I create two points with the same coordinate type and dimension, I can assign them.<\/p>\n<p>Now the misconception begins. You can assign an <span style=\"font-family: courier new, courier;\">int<\/span> to a <span style=\"font-family: courier new, courier;\">double <\/span>(line 3). Therefore, assigning a Point of ints to the point of doubles should be possible. The C++ compiler is quite specific about line 4. Both class templates are not related and cannot be assigned. They are different types.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5635\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment.png\" alt=\"genericAssignment\" width=\"700\" height=\"199\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment.png 1160w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment-300x86.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment-1024x293.png 1024w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The error message gives the first hint. I need an assignment operator that supports the conversion from <span style=\"font-family: courier new, courier;\">Point&lt;int, 3&gt;<\/span> to <span style=\"font-family: courier new, courier;\">Point&lt;double, 3&gt;<\/span>. The class template now has a generic copy assignment operator.<\/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;\">\/\/ genericAssignment2.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&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: #007788; font-weight: bold;\">int<\/span> N<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Point{\r\n\r\n    Point(std<span style=\"color: #555555;\">::<\/span>initializer_list<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> initList)<span style=\"color: #555555;\">:<\/span> coord(initList){}\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> T2<span style=\"color: #555555;\">&gt;<\/span>                        \r\n    Point<span style=\"color: #555555;\">&lt;<\/span>T, N<span style=\"color: #555555;\">&gt;&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Point<span style=\"color: #555555;\">&lt;<\/span>T2, N<span style=\"color: #555555;\">&gt;&amp;<\/span> point){     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        static_assert(std<span style=\"color: #555555;\">::<\/span>is_convertible<span style=\"color: #555555;\">&lt;<\/span>T2, T<span style=\"color: #555555;\">&gt;::<\/span>value, \r\n                      <span style=\"color: #cc3300;\">\"Cannot convert source type to destination type!\"<\/span>);\r\n        coord.clear();\r\n        coord.insert(coord.begin(), point.coord.begin(), point.coord.end());\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>;\r\n    }\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span> coord;\r\n    \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  Point<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> point1{<span style=\"color: #ff6600;\">1.1<\/span>, <span style=\"color: #ff6600;\">2.2<\/span>, <span style=\"color: #ff6600;\">3.3<\/span>};\r\n  Point<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> point2{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};\r\n  \r\n  Point<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">&gt;<\/span> point3{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>};\r\n  Point<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;<\/span> point4{<span style=\"color: #cc3300;\">\"Only\"<\/span>, <span style=\"color: #cc3300;\">\"a\"<\/span>, <span style=\"color: #cc3300;\">\"test\"<\/span>};\r\n\r\n  point1 <span style=\"color: #555555;\">=<\/span> point2;<span style=\"color: #0099ff; font-style: italic;\">                                        \/\/ (3)<\/span>\r\n  \r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ point2 = point3;                                     \/\/ (4)<\/span>\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ point2 = point4;                                     \/\/ (5)<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Due to line (1), the copy assignment in line (3) works. Let&#8217;s have a closer look at the class template <span style=\"font-family: courier new, courier;\">Point:<\/span>&nbsp;<\/p>\n<ul>\n<li><span style=\"font-family: courier new, courier; color: #000000;\">Point&lt;T, N&gt;&amp; operator=(const Point&lt;T2, N&gt;&amp; point): <\/span><span style=\"color: #000000;\">The assigned to <span style=\"font-family: courier new, courier;\">Point<\/span> is of type <span style=\"font-family: courier new, courier;\">Point&lt;T, N&gt;<\/span> and accepts only the <span style=\"font-family: courier new, courier;\">Point,<\/span> which has the same dimension, but the type could vary:<span style=\"font-family: courier new, courier;\"> Point&lt;T2, N&gt;<\/span>.<\/span><span style=\"font-family: courier new, courier; color: #000000;\"> <\/span><\/li>\n<li><span style=\"font-family: courier new, courier; color: #000000;\">static_assert(std::is_convertible&lt;T2, T&gt;::value, &#8220;Cannot convert source type to destination type!&#8221;): <\/span><span style=\"color: #000000;\">This expression checks with the help of the function <span style=\"font-family: courier new, courier;\">std::is_convertible<\/span> from the type-traits library, if T2 can be converted to T.<\/span><\/li>\n<\/ul>\n<p>When I use lines (4) and (5), the compilation fails:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5636\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment2.png\" alt=\"genericAssignment2\" width=\"700\" height=\"251\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment2.png 1517w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment2-300x107.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment2-1024x367.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/genericAssignment2-768x275.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>Line (3) gives an error because both points have different dimensions. Line (4) triggers the <span style=\"font-family: courier new, courier;\">static_assert<\/span> in the assignment operator because a <span style=\"font-family: courier new, courier;\">std::string<\/span> is not convertible to an <span style=\"font-family: courier new, courier;\">int.<\/span><\/p>\n<p>I assume the next misconception has more surprise potential.<\/p>\n<\/p>\n<h2>Methods inherited from Class Templates are per se not available<\/h2>\n<p>Let&#8217;s start simple.<\/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;\">\/\/ inheritance.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Base<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> func(){                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"func\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Derived<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Base{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> callBase(){\r\n        func();                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/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    Derived derived;\r\n    derived.callBase();              \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>I implemented a class, <span style=\"font-family: courier new, courier;\">Base<\/span>, and <span style=\"font-family: courier new, courier;\">Derived<\/span>. Derived is public derived from <span style=\"font-family: courier new, courier;\">Base<\/span> and can, therefore, be used in its method <span style=\"font-family: courier new, courier;\">callBase<\/span> (line 2), the method <span style=\"font-family: courier new, courier;\">func<\/span> from class <span style=\"font-family: courier new, courier;\">Base<\/span>. Okay, I have nothing to add to the output of the program.<span style=\"font-family: courier new, courier;\"><\/span><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5637\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/inheritance.png\" alt=\"inheritance\" width=\"300\" height=\"147\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/inheritance.png 419w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/inheritance-300x147.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Making <span style=\"font-family: courier new, courier;\">Base<\/span> a class template changes the behavior.<\/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;\">\/\/ templateInheritance.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&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: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Base<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> func(){                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"func\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n};\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: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Derived<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Base<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> callBase(){\r\n        func();                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/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    Derived<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> derived;\r\n    derived.callBase();              \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>&nbsp;I assume the compiler error may surprise you.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5638\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateInheritance1.png\" alt=\"templateInheritance1\" width=\"600\" height=\"193\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateInheritance1.png 824w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateInheritance1-300x96.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateInheritance1-768x247.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>The line &#8220;<span style=\"font-family: courier new, courier;\">there are no arguments to &#8216;func&#8217; that depend on a template parameter, so a declaration of &#8216;func&#8217; must be available<\/span>&#8221; from the error message gives the first hint. <span style=\"font-family: courier new, courier;\">func<\/span> is a so-called non-dependent name because its name does not depend on the template parameter T. The consequence is that the compiler does not look in the from T dependent base class <span style=\"font-family: courier new, courier;\">Base&lt;T&gt;<\/span>, and there is no name <span style=\"font-family: courier new, courier;\">func<\/span> available outside the class template.<\/p>\n<p>There are three workarounds to extend the name lookup to the dependent base class. The following example uses all three.<\/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;\">\/\/ templateInheritance2.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&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: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Base<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> func1() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"func1()\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  }\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> func2() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"func2()\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  }\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> func3() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"func3()\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  }\r\n};\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: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Derived<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Base<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">using<\/span> Base<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>func2;              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">callAllBaseFunctions<\/span>(){\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">this<\/span><span style=\"color: #555555;\">-&gt;<\/span>func1();                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    func2();                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    Base<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>func3();                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n\r\n  }\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  Derived<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> derived;\r\n  derived.callAllBaseFunctions();\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<ul>\n<li><strong>Make the name&nbsp;dependent<\/strong>: The call <span style=\"font-family: courier new, courier;\">this-&gt;func1<\/span> in line 1 is dependent because <span style=\"font-family: courier new, courier;\">this<\/span> is implicit dependent. The name lookup will consider, in this case, all base classes.<\/li>\n<li><strong>Introduce the name&nbsp;into the current scope:<\/strong> The expression <span style=\"font-family: courier new, courier;\">using Base&lt;T&gt;::func2<\/span> (line 2) introduces <span style=\"font-family: courier new, courier;\">func2<\/span> into the current scope.<\/li>\n<li><strong>Call the name&nbsp;fully qualified<\/strong>: Calling <span style=\"font-family: courier new, courier;\">func3<\/span> fully qualified (line 3) will break a virtual dispatch and may cause new surprises.<\/li>\n<\/ul>\n<p>In the end, here is the output of the program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5639\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateInheritance2.png\" alt=\"templateInheritance2\" width=\"300\" height=\"161\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateInheritance2.png 445w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/02\/templateInheritance2-300x161.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>I have more to write about dependent names in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-other-template-rules\">next post<\/a>. Sometimes you have to disambiguate dependent names with <span style=\"font-family: courier new, courier;\">typename<\/span> or <span style=\"font-family: courier new, courier;\">template.<\/span> <span class=\"tlid-translation translation\"><span class=\"\" title=\"\">If you see this for the first time, you are probably as surprised as me.<\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often teach the basics of templates. Templates are special. Therefore, I encounter many misconceptions which cause surprises. Here are a few of them.<\/p>\n","protected":false},"author":21,"featured_media":5634,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5640","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\/5640","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=5640"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5640\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5634"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}