{"id":6522,"date":"2023-03-05T17:18:28","date_gmt":"2023-03-05T17:18:28","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/idioms-for-polymorphism-and-templates\/"},"modified":"2023-03-05T17:18:28","modified_gmt":"2023-03-05T17:18:28","slug":"idioms-for-polymorphism-and-templates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/idioms-for-polymorphism-and-templates\/","title":{"rendered":"Idioms for Polymorphism and Templates"},"content":{"rendered":"<p>This post is unique because I have written about all the topics mentioned in this post already. Therefore, I only provide a few words about the topic and a link to the post mentioned.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6521\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/IdiomsPolymorphismTemplates.png\" alt=\"IdiomsPolymorphismTemplates\" width=\"650\" height=\"329\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/IdiomsPolymorphismTemplates.png 1233w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/IdiomsPolymorphismTemplates-300x152.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/IdiomsPolymorphismTemplates-1024x518.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/IdiomsPolymorphismTemplates-768x389.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I decided on this particular post for two reasons. First, all the idioms for polymorphism and templates in C++ are very important and often used. Second, I don&#8217;t write posts about topics I have already a few months ago written about.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Polymorphism\"><\/span>Polymorphism<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Polymorphism is the property that different types support the same interface. In C++, we distinguish between dynamic polymorphism and static polymorphism.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Dynamic_Polymorphism\"><\/span>Dynamic Polymorphism<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Dynamic Polymorphism takes place at run time, based on object orientation, and enables us to separate between the interface and the implementation of a class hierarchy. To get late binding, dynamic dispatch, or dispatch at run time, you need virtuality and an indirection such as a pointer or a reference.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/dynamic-and-static-polymorphism\">Dynamic versus Static Polymorphism<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Curiously_Recurring_Template_Pattern_CRTP\"><\/span>Curiously Recurring Template Pattern (CRTP)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Here is the crucial idea of CRTP: A class <code>Derived<\/code> derives from a class template <code>Base<\/code>, and <code>Base<\/code> has <code>Derived<\/code> as a template argument.<\/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: #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    ...\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<span style=\"color: #555555;\">&lt;<\/span>Derived<span style=\"color: #555555;\">&gt;<\/span> {\r\n    ...\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/dynamic-and-static-polymorphism\">More about Dynamic versus Static Polymorphism<\/a><\/li>\n<\/ul>\n<p>Static polymorphism is based on CRTP.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Static_Polymorphism\"><\/span>Static Polymorphism<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Static polymorphism happens at compile time and has no runtime performance cost.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/dynamic-and-static-polymorphism\">More about Dynamic versus Static Polymorphism<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"The_Overload_Pattern\"><\/span>The Overload Pattern<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/variant\/visit\"><code><\/code><\/a>Typically, you use the overload pattern for a <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/variant\"><code>std::variant<\/code><\/a>.<code> std::variant<\/code> is a type-safe union. with one value from one of its types. <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/variant\/visit\">std::visit <\/a>allows you to apply a visitor to it. Exactly here comes the Overload Pattern in C++20 very handy in play.<\/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: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> ... Ts<span style=\"color: #555555;\">&gt;<\/span> \r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Overload <span style=\"color: #555555;\">:<\/span> Ts ... { \r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> Ts<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">operator<\/span>() ... ; \r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/smart-tricks-with-fold-expressions\">Smart Tricks with Parameter Packs and Fold Expressions<\/a><\/li>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/visiting-a-std-variant-with-the-overload-pattern\">Visiting a std::variant with the Overload Pattern<\/a><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Templates\"><\/span>Templates<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Templates extend C++ with many new idioms.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Mixins\"><\/span>Mixins<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Mixin\">Mixins<\/a> are a popular idea in the design of classes to mix in new code. You can implement mixins in C++ by using CRTP. A prominent example is the class<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/memory\/enable_shared_from_this\"> std::enable_shared_from_this<\/a>.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/mixins\">Mixins<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Expression_Templates\"><\/span>Expression Templates<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Expression templates are typically used in linear algebra and are&nbsp; &#8220;structures representing a computation at compile-time, which structures are evaluated only as needed to produce efficient code for the entire computation&#8221; (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Expression_templates\">https:\/\/en.wikipedia.org\/wiki\/Expression_templates<\/a>). In other words, expression templates are only evaluated when needed.&nbsp;<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/avoiding-temporaries-with-expression-templates\">Avoiding Temporaries with Expression Templates<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Policy\"><\/span>Policy<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A policy is a generic function or class whose behavior can be configured. Typically, there are default values for the policy parameters.<code> std::vector<\/code> and <code>std::unordered_map<\/code> exemplify this design idea in the Standard Template Library.<\/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: #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: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Allocator<\/span> std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&gt;<\/span>        \r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">vector<\/span>; \r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Key<\/span>,\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T<\/span>,\r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Hash<\/span> <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>hash<span style=\"color: #555555;\">&lt;<\/span>Key<span style=\"color: #555555;\">&gt;<\/span>,                               \r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">KeyEqual<\/span> <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>equal_to<span style=\"color: #555555;\">&lt;<\/span>Key<span style=\"color: #555555;\">&gt;<\/span>,                       \r\n    <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">allocator<\/span> <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>allocator<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>pair<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">const<\/span> Key, T<span style=\"color: #555555;\">&gt;&gt;<\/span>  \r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">unordered_map<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/policy-and-traits\">Policy<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Traits\"><\/span>Traits<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Traits are class templates that provide characteristics of a generic type. They can extract one or more characteristics of a class template.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/softwaredesign-with-traits-and-tag-dispatching\">Software Design with Traits and Tag Dispatching<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Tag_Dispatching\"><\/span>Tag Dispatching<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Tag dispatching is a way to simulate function overloading based on concepts, often based on traits.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/softwaredesign-with-traits-and-tag-dispatching\">Software Design with Traits and Tag Dispatching<\/a><\/li>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/a-std-advance-implementation-with-c-98-c-17-and-c-20\">A std::advance Implementation with C++98, C++17, and C++20<\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Type_Erasure\"><\/span>Type Erasure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Type Erasure enables using various concrete types through a single generic interface. In C, you base it on void pointers; in C++, on object orientation or templates.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/type-erasure\">Type Erasure<\/a><\/li>\n<\/ul>\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>I&#8217;m happy to present in my next post a guest post from Alex Eisenhut. Alex will write about his passion: good software architecture.<\/p>\n<p>&nbsp;<\/p>\n<\/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>This post is unique because I have written about all the topics mentioned in this post already. Therefore, I only provide a few words about the topic and a link to the post mentioned.<\/p>\n","protected":false},"author":21,"featured_media":6521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6522","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6522","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=6522"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6522\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6521"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}