{"id":5571,"date":"2018-12-03T17:25:54","date_gmt":"2018-12-03T17:25:54","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-template-definitons\/"},"modified":"2018-12-03T17:25:54","modified_gmt":"2018-12-03T17:25:54","slug":"c-core-guidelines-template-definitons","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-template-definitons\/","title":{"rendered":"C++ Core Guidelines: Template Definitions"},"content":{"rendered":"<p>Template definitions deal with guidelines that are specific to a template implementation. This means, in particular, these rules focus on how a template definition depends on its context.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5567\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/super-charged-engine-2770374_1280.jpg\" alt=\"super charged engine 2770374 1280\" width=\"600\" height=\"400\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/super-charged-engine-2770374_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/super-charged-engine-2770374_1280-300x200.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/super-charged-engine-2770374_1280-1024x682.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/super-charged-engine-2770374_1280-768x512.jpg 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;Here are today&#8217;s rules for template definitions:<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-depend\">T.60: Minimize a template\u2019s context dependencies<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-scary\">T.61: Do not over-parameterize members (SCARY)<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-nondependent\">T.62: Place non-dependent class template members in a non-templated base class<\/a><\/li>\n<\/ul>\n<p>The first rule is quite special.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"T60_Minimize_a_templates_context_dependencies\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-depend\">T.60: Minimize a template\u2019s context dependencies<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Honestly, it took me a few moments to get this rule. Let&#8217;s look at the function templates <span style=\"font-family: courier new, courier;\">sort<\/span> and <span style=\"font-family: courier new, courier;\">algo.<\/span> Here is a simplified example of the guidelines.<\/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> C<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> sort(C<span style=\"color: #555555;\">&amp;<\/span> c)\r\n{\r\n    std<span style=\"color: #555555;\">::<\/span>sort(begin(c), end(c)); <span style=\"color: #0099ff; font-style: italic;\">\/\/ necessary and useful dependency<\/span>\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> Iter<span style=\"color: #555555;\">&gt;<\/span>\r\nIter algo(Iter first, Iter last) {\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (; first <span style=\"color: #555555;\">!=<\/span> last; <span style=\"color: #555555;\">++<\/span>first) {\r\n        <span style=\"color: #006699; font-weight: bold;\">auto<\/span> x <span style=\"color: #555555;\">=<\/span> sqrt(<span style=\"color: #555555;\">*<\/span>first); <span style=\"color: #0099ff; font-style: italic;\">\/\/ potentially surprising dependency: which sqrt()?<\/span>\r\n        helper(first, x);      <span style=\"color: #0099ff; font-style: italic;\">\/\/ potentially surprising dependency:<\/span>\r\n                               <span style=\"color: #0099ff; font-style: italic;\">\/\/ helper is chosen based on first and x    <\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>It would be optimal but not always manageable if a template operates only on its arguments. This holds for the function template <span style=\"font-family: courier new, courier;\">sort<\/span> but not for <span style=\"font-family: courier new, courier;\">algo.<\/span>The function template <span style=\"font-family: courier new, courier;\">algo<\/span> has dependencies to <span style=\"font-family: courier new, courier;\">sqrt<\/span> and the function <span style=\"font-family: courier new, courier;\">helper. <\/span>Ultimately, the implementation of <span style=\"font-family: courier new, courier;\">algo<\/span> introduces more dependencies than the interface shows.<span style=\"font-family: courier new, courier;\"><br \/><\/span><\/p>\n<\/p>\n<h3><span class=\"ez-toc-section\" id=\"T61_Do_not_over-parameterize_members_SCARY\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-scary\">T.61: Do not over-parameterize members (SCARY)<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If a template member does not depend on a template parameter, it removes from the template. A member may be a type or a method. By following this rule, you may decrease the code size because the non-generic code is factored out.<\/p>\n<p>The example from the guidelines is relatively straightforward.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> A <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator{}<span style=\"color: #555555;\">&gt;<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ requires Regular&lt;T&gt; &amp;&amp; Allocator&lt;A&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">List<\/span> {\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">struct<\/span> Link {   <span style=\"color: #0099ff; font-style: italic;\">\/\/ does not depend on A<\/span>\r\n        T elem;\r\n        T<span style=\"color: #555555;\">*<\/span> pre;\r\n        T<span style=\"color: #555555;\">*<\/span> suc;\r\n    };\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> iterator <span style=\"color: #555555;\">=<\/span> Link<span style=\"color: #555555;\">*<\/span>;\r\n\r\n    iterator first() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> head; }\r\n\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    Link<span style=\"color: #555555;\">*<\/span> head;\r\n};\r\n\r\nList<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> lst1;\r\nList<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, My_allocator<span style=\"color: #555555;\">&gt;<\/span> lst2;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The type <span style=\"font-family: courier new, courier;\">Link<\/span> does not depend on the template parameter <span style=\"font-family: courier new, courier;\">A<\/span>. So I can remove it and use it in <span style=\"font-family: courier new, courier;\">List2.<\/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%;\">template<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> Link {\r\n    T elem;\r\n    T<span style=\"color: #555555;\">*<\/span> pre;\r\n    T<span style=\"color: #555555;\">*<\/span> suc;\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: #006699; font-weight: bold;\">typename<\/span> A <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator{}<span style=\"color: #555555;\">&gt;<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ requires Regular&lt;T&gt; &amp;&amp; Allocator&lt;A&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">List2<\/span> {\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> iterator <span style=\"color: #555555;\">=<\/span> Link<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;*<\/span>;\r\n\r\n    iterator first() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> head; }\r\n\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    Link<span style=\"color: #555555;\">*<\/span> head;\r\n};\r\n\r\nList<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> lst1;\r\nList<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, My_allocator<span style=\"color: #555555;\">&gt;<\/span> lst2;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Was this an easy job? Yes? No! The title of this rule is about SCARY. What does that mean? I&#8217;m curious, but honestly, you can ignore the following lines.<\/p>\n<p>The acronym <strong>SCARY<\/strong> describes assignments and initializations that are <strong>S<\/strong>eemingly erroneous (appearing <strong>C<\/strong>onstrained by conflicting generic parameters) but <strong>A<\/strong>ctually work with the <strong>R<\/strong>ight implementation (unconstrained b<strong>Y<\/strong> the conflict due to minimized dependencies).<\/p>\n<p>I hope you get it. For the details, see <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/WG21\/docs\/papers\/2009\/n2911.pdf\">N2911<\/a>. In order not to bore you, here is the apparent idea applied to the standard container iterator: they have no dependency on the containers,&nbsp;<span style=\"font-family: courier new, courier;\"><code class=\"computeroutput\"><span class=\"identifier\">key_compare<\/span><\/code><code class=\"computeroutput\"><span class=\"identifier\"><\/span><\/code><\/span><span style=\"font-family: courier new, courier;\"><code class=\"computeroutput\"><span class=\"identifier\"><span style=\"font-family: courier new, courier;\">,<\/span> hasher<\/span><\/code>, <code class=\"computeroutput\"><span class=\"identifier\">key_equal<\/span><\/code>,<\/span> or <span style=\"font-family: courier new, courier;\"><code class=\"computeroutput\"><span class=\"identifier\">allocator<\/span><\/code><\/span> types.<\/p>\n<p>The next rule helps to reduce code bloat.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"T62_Place_non-dependent_class_template_members_in_a_non-templated_base_class\"><\/span><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-nondependent\">T.62: Place non-dependent class template members in a non-templated base class<\/a><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Let me say it more informally: put the template&#8217;s functionality which does not depend on the template parameters, in a non-templated base class.<\/p>\n<p>The guidelines present a quite obvious example.<\/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;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Foo<\/span> {\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">enum<\/span> { v1, v2 };\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The enumeration is independent of the type parameter T and should, therefore, be placed in a non-templated base class.<\/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;\">struct<\/span> Foo_base {\r\n    <span style=\"color: #006699; font-weight: bold;\">enum<\/span> { v1, v2 };\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\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;\">Foo<\/span> <span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Foo_base {\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, <span style=\"font-family: courier new, courier;\">Foo_base<\/span> can be used without template arguments and template instantiation.<\/p>\n<p>This technique is quite interesting if you want to reduce your code size. Here is a simple class template <span style=\"font-family: courier new, courier;\">Array.<\/span><\/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;\">\/\/ genericArray.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cstddef&gt;<\/span>\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, std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> N<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Array<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    Array()<span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n    <span style=\"color: #007788; font-weight: bold;\">std::size_t<\/span> getSize() <span style=\"color: #006699; font-weight: bold;\">const<\/span>{\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> N;\r\n    }\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n  T elem[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    Array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">100<\/span><span style=\"color: #555555;\">&gt;<\/span> arr1;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"arr1.getSize(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> arr1.getSize() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    Array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">200<\/span><span style=\"color: #555555;\">&gt;<\/span> arr2;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"arr2.getSize(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> arr2.getSize() <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>If you study the class template <span style=\"font-family: courier new, courier;\">Array,<\/span> you will see that the method <span style=\"font-family: courier new, courier;\">getSize<\/span> is the same except for the type parameter N. Let me refactor the code and declare a class template <span style=\"font-family: courier new, courier;\">Array<\/span> that depends on the type parameter <span style=\"font-family: courier new, courier;\">T.<\/span><\/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;\">\/\/ genericArrayInheritance.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cstddef&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\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;\">ArrayBase<\/span> {\r\n<span style=\"color: #9999ff;\">protected:<\/span>\r\n    ArrayBase(std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> n)<span style=\"color: #555555;\">:<\/span> size(n) {} \r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> getSize() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> size;\r\n    };\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> size;\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, std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> n<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Array<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">private<\/span> ArrayBase<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>    \r\n    Array()<span style=\"color: #555555;\">:<\/span> ArrayBase<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>(n){}\r\n    std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> getSize() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span>  ArrayBase<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;::<\/span>getSize();\r\n    }\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    T data[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    Array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">100<\/span><span style=\"color: #555555;\">&gt;<\/span> arr1;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"arr1.getSize(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> arr1.getSize() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    Array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">200<\/span><span style=\"color: #555555;\">&gt;<\/span> arr2;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"arr2.getSize(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> arr2.getSize() <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><span style=\"font-family: courier new, courier;\">Array<\/span> has two template parameters for the type <span style=\"font-family: courier new, courier;\">T<\/span> and the size <span style=\"font-family: courier new, courier;\">n<\/span>, but <span style=\"font-family: courier new, courier;\">ArrayBase<\/span> only has one template parameter for the type <span style=\"font-family: courier new, courier;\">T<\/span>. <span style=\"font-family: courier new, courier;\">Array<\/span> derives from <span style=\"font-family: courier new, courier;\">ArrayBase.<\/span><span style=\"font-family: courier new, courier;\"> <\/span>This means <span style=\"font-family: courier new, courier;\">ArrayBase<\/span> is shared between all instantiations of <span style=\"font-family: courier new, courier;\">Array<\/span>, which uses the same type T. In the concrete case, the <span style=\"font-family: courier new, courier;\">getSize<\/span> method of <span style=\"font-family: courier new, courier;\">Array<\/span> uses the <span style=\"font-family: courier new, courier;\">getSize<\/span> method of <span style=\"font-family: courier new, courier;\">ArrayBase.<\/span><\/p>\n<p>Thanks to <a href=\"https:\/\/cppinsights.io\/\">CppInsight<\/a>, I can show you the compiler-generated code.<\/p>\n<p>Here is the instantiation of<span style=\"font-family: courier new, courier;\"> ArrayBase&lt;int&gt;<\/span>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5568\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/ArrayBase.png\" alt=\"ArrayBase\" width=\"500\" height=\"418\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/ArrayBase.png 546w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/ArrayBase-300x251.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>And here the instantiation for <span style=\"font-family: courier new, courier;\">Array&lt;int, 100&gt;<\/span> and <span style=\"font-family: courier new, courier;\">Array&lt;int, 200&gt;<\/span>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignleft size-full wp-image-5569\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Array.png\" alt=\"Array\" width=\"400\" height=\"345\" style=\"float: left;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Array.png 506w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Array-300x258.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignright size-full wp-image-5570\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Array200.png\" alt=\"Array200\" width=\"400\" height=\"357\" style=\"float: right;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Array200.png 495w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Array200-300x268.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\"><\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"i\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"i-2\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"i-3\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"i-4\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"i-5\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"i-6\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"i-7\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"i-8\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h2><span class=\"ez-toc-section\" id=\"i-9\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h2><span class=\"ez-toc-section\" id=\"Whats_next\"><\/span>What&#8217;s next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Of course, there are more rules left to template definitions. So my story continues with the<a href=\"http:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-template-definitons\"> next post<\/a>. I hope my explanation of template definitions is good enough because many programmers fear templates. To me, the ideas of templates are easy to get, but the syntax still has potential.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Template definitions deal with guidelines that are specific to a template implementation. This means, in particular, these rules focus on how a template definition depends on its context.<\/p>\n","protected":false},"author":21,"featured_media":5567,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-5571","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\/5571","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=5571"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5571\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5567"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}