{"id":5586,"date":"2018-12-16T09:04:03","date_gmt":"2018-12-16T09:04:03","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-templates-and-hierarchies\/"},"modified":"2023-06-26T10:17:27","modified_gmt":"2023-06-26T10:17:27","slug":"c-core-guidelines-rules-for-templates-and-hierarchies","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-templates-and-hierarchies\/","title":{"rendered":"C++ Core Guidelines: Rules for Templates and Hierarchies"},"content":{"rendered":"<p>Due to the&nbsp; C++ core guidelines, &#8220;Templates are the backbone of C++\u2019s support for generic programming and class hierarchies the backbone of its support for object-oriented programming. The two language mechanisms can be combined effectively, but a few design pitfalls must be avoided.&#8221; Let me see what this means.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5579\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/hierarchy-35795_1280.png\" alt=\"hierarchy 35795 1280\" width=\"400\" height=\"328\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/hierarchy-35795_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/hierarchy-35795_1280-300x246.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/hierarchy-35795_1280-1024x841.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/hierarchy-35795_1280-768x631.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>This section consists of five rules.<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-hier\">T.80: Do not naively templatize a class hierarchy<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-array\">T.81: Do not mix hierarchies and arrays<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-linear\">T.82: Linearize a hierarchy when virtual functions are undesirable<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-virtual\">T.83: Do not declare a member function template virtual<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-abi\">T.84: Use a non-template core implementation to provide an ABI-stable interface<\/a><\/li>\n<\/ul>\n<p>Rule T.81 is too loosely related to templates and hierarchies, and rule T.82 is empty; therefore, my post boils down to the three remaining rules.<\/p>\n<p>I will write about the rules T.80 and T.84 together because T.84 continues the story of T.80.<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-hier\">T.80: Do not naively templatize a class hierarchy, <\/a>and <a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-abi\">T.84: Use a non-template core implementation to provide an ABI-stable interface<\/a><\/h2>\n<p>Here is an example a naively templatized class hierarchy from the guidelines:<\/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> Container {         <span style=\"color: #0099ff; font-style: italic;\">\/\/ an interface<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> T<span style=\"color: #555555;\">*<\/span> get(<span style=\"color: #007788; font-weight: bold;\">int<\/span> i);\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> T<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">first<\/span>();\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> T<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">next<\/span>();\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">sort<\/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;\">Vector<\/span> <span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Container<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: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n};\r\n\r\nVector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> vi;\r\nVector<span style=\"color: #555555;\">&lt;<\/span>string<span style=\"color: #555555;\">&gt;<\/span> vs;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Why is this due to the guidelines naively? This is, in particular, naively because the base class <span style=\"font-family: courier new, courier;\">Container<\/span> has many virtual functions. The presented design introduces code bloat. Virtual functions are instantiated every time in a class template. In contrast, non-virtual functions are only instantiated if they are used.<\/p>\n<p>A simple test with <a href=\"https:\/\/cppinsights.io\/\">CppInsight<\/a> proves my point.<\/p>\n<h3>Non-virtual functions<\/h3>\n<p>The program uses a <span style=\"font-family: courier new, courier;\">std::vector&lt;int&gt;<\/span> and a <span style=\"font-family: courier new, courier;\">std::vector&lt;std::string&gt;<\/span>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5580\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector1.png\" alt=\"Vector1\" width=\"300\" height=\"128\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector1.png 489w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector1-300x128.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>CppInsight shows it. No method of <span style=\"font-family: courier new, courier;\">std::vector<\/span> is instantiated.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5581\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2.png\" alt=\"Vector2\" width=\"500\" height=\"101\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2.png 859w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2-300x60.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2-768x155.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h3>Virtual functions<\/h3>\n<p>Here is the simplified program of the C++ core guidelines, including the virtual function <span style=\"font-family: courier new, courier;\">sort.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5582\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector1Virtual.png\" alt=\"Vector1Virtual\" width=\"200\" height=\"228\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector1Virtual.png 284w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector1Virtual-263x300.png 263w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/p>\n<p>Now, the virtual function <span style=\"font-family: courier new, courier;\">sort<\/span> is instantiated. Here is only the output of CppInsight, which shows the instantiation of the class template <span style=\"font-family: courier new, courier;\">Container. <\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5583\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2Virtual.png\" alt=\"Vector2Virtual\" width=\"600\" height=\"432\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2Virtual.png 802w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2Virtual-300x216.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/Vector2Virtual-768x553.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In total, I get 100 lines of code for the virtual function.<\/p>\n<p>The note to the guidelines hints at how to overcome this code bloat. Often you can provide a stable interface by not parameterizing a base. This brings me to the related rule <a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-abi\">T.84: Use a non-template core implementation to provide an ABI-stable interface<\/a>.<\/p>\n<p>Okay, I already have written about this technique in the post <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-template-definitons\">C++ Core Guidelines: Template Definitions<\/a>. Rule T.84 mentions an alternative way to address a stable interface: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-template-definitons\">Pimpl. <\/a><\/p>\n<h3>Pimpl<\/h3>\n<p>Pimpl stands for &#8220;<strong>p<\/strong>ointer to <strong>impl<\/strong>ementation&#8221; and means to remove implementation details of a class by placing them in a separate class, accessed through a pointer. This technique should be in the toolbox of each serious C++ programmer. Pimpl is often also called a compilation firewall because this technique breaks the dependency between the implementation and the users of the class interface. This means the implementation can be changed without recompiling the user code.<\/p>\n<p>Here is the general structure from Herb Sutters Blog: &nbsp;<span class=\"reference-text\"><a href=\"http:\/\/herbsutter.com\/gotw\/_100\/\" rel=\"nofollow\" class=\"external text\">GotW #100<\/a>: Compilation Firewalls. <\/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;\">\/\/ in header file<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">widget<\/span> {\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    widget();\r\n    <span style=\"color: #555555;\">~<\/span>widget();\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">impl<\/span>;                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    unique_ptr<span style=\"color: #555555;\">&lt;<\/span>impl<span style=\"color: #555555;\">&gt;<\/span> pimpl;\r\n};\r\n \r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ in implementation file                               \/\/ (2)                 <\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">widget<\/span><span style=\"color: #555555;\">::<\/span>impl {\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ :::<\/span>\r\n};\r\n \r\nwidget<span style=\"color: #555555;\">::<\/span>widget() <span style=\"color: #555555;\">:<\/span> pimpl{ <span style=\"color: #006699; font-weight: bold;\">new<\/span> impl{ <span style=\"color: #0099ff; font-style: italic;\">\/*...*\/<\/span> } } { }\r\nwidget<span style=\"color: #555555;\">::~<\/span>widget() { }  \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>These are the points of this idiom.<code><\/code><\/p>\n<ol>\n<li>put all private non-virtual members into <span style=\"font-family: courier new, courier;\">impl<\/span> (1)<\/li>\n<li>forward declare <span style=\"font-family: courier new, courier;\">impl <br \/><\/span><\/li>\n<li>define <span style=\"font-family: courier new, courier;\">impl<\/span> in the corresponding implementation file (2)<code><\/code><\/li>\n<\/ol>\n<p>Okay. Let me show a full example based on the one from <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/pimpl\">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: #0099ff; font-style: italic;\">\/\/ pimpl.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;memory&gt;<\/span>\r\n \r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ interface (widget.h)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">widget<\/span> {\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">impl<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>impl<span style=\"color: #555555;\">&gt;<\/span> pImpl;\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">draw<\/span>();\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> shown() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">true<\/span>; } \r\n    widget(<span style=\"color: #007788; font-weight: bold;\">int<\/span>);\r\n    <span style=\"color: #555555;\">~<\/span>widget(); \r\n    widget(widget<span style=\"color: #555555;\">&amp;&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;  \r\n    widget(<span style=\"color: #006699; font-weight: bold;\">const<\/span> widget<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n    widget<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(widget<span style=\"color: #555555;\">&amp;&amp;<\/span>); \r\n    widget<span style=\"color: #555555;\">&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> widget<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">delete<\/span>;\r\n};\r\n \r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ implementation (widget.cpp)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">widget<\/span><span style=\"color: #555555;\">::<\/span>impl {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> n; <span style=\"color: #0099ff; font-style: italic;\">\/\/ private data<\/span>\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">draw<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> widget<span style=\"color: #555555;\">&amp;<\/span> w) {                             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span>(w.shown())\r\n            std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"drawing a widget \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> n <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n    impl(<span style=\"color: #007788; font-weight: bold;\">int<\/span> n) <span style=\"color: #555555;\">:<\/span> n(n) {}\r\n};\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> widget<span style=\"color: #555555;\">::<\/span>draw() { pImpl<span style=\"color: #555555;\">-&gt;<\/span>draw(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>); }                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\nwidget<span style=\"color: #555555;\">::<\/span>widget(<span style=\"color: #007788; font-weight: bold;\">int<\/span> n) <span style=\"color: #555555;\">:<\/span> pImpl{std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>impl<span style=\"color: #555555;\">&gt;<\/span>(n)} {}\r\nwidget<span style=\"color: #555555;\">::~<\/span>widget() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;                                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\nwidget<span style=\"color: #555555;\">&amp;<\/span> widget<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(widget<span style=\"color: #555555;\">&amp;&amp;<\/span>) <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n \r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ user (main.cpp)<\/span>\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\t\r\n    widget w(<span style=\"color: #ff6600;\">7<\/span>);\r\n    w.draw();\r\n\t\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>The <span style=\"font-family: courier new, courier;\">draw<\/span> call of the class <span style=\"font-family: courier new, courier;\">impl<\/span> uses a back-reference to <span style=\"font-family: courier new, courier;\">widget<\/span> (lines (1) and (2)). The destructor and the move assignment operator (line (3)) must be user-defined in the implementation file base because <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/memory\/unique_ptr\" title=\"cpp\/memory\/unique ptr\">std::unique_ptr<\/a><\/span> requires that the pointed-to type is complete.<\/p>\n<p>The program behaves as expected:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5584\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/pimpl.png\" alt=\"pimpl\" width=\"250\" height=\"172\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/pimpl.png 431w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/pimpl-300x206.png 300w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/p>\n<p>Besides its pros, the pimpl idiom has two cons: there is an additional pointer indirection, and you have to store the pointer.<\/p>\n<p>The last guideline for this post is about a typical misconception.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rt-virtual\">T.83: Do not declare a member function template virtual<\/a><\/h2>\n<p>Let me try to use a virtual member function template.<\/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;\">\/\/ virtualMember.cpp<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Shape<\/span> {\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>\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> intersect(T<span style=\"color: #555555;\">*<\/span> p);\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    Shape shape;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;The error message from my GCC 8.2 compiler is crystal-clear:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5585\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/virtualMember.png\" alt=\"virtualMember\" width=\"500\" height=\"153\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/virtualMember.png 981w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/virtualMember-300x92.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/12\/virtualMember-768x235.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The next guidelines and, therefore, my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-variadic-templates\">next post<\/a> is about variadic templates. Variadic templates are templates that can accept an arbitrary number of arguments. The rules in the guidelines for variadic templates, as many rules to templates, consist only of the headings. This means I will write more generally about variadic templates in my next post.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Due to the&nbsp; C++ core guidelines, &#8220;Templates are the backbone of C++\u2019s support for generic programming and class hierarchies the backbone of its support for object-oriented programming. The two language mechanisms can be combined effectively, but a few design pitfalls must be avoided.&#8221; Let me see what this means.<\/p>\n","protected":false},"author":21,"featured_media":5579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[422],"class_list":["post-5586","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-tag-dispatching"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5586","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=5586"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5586\/revisions"}],"predecessor-version":[{"id":6801,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5586\/revisions\/6801"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5579"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}