{"id":6315,"date":"2022-03-02T08:35:29","date_gmt":"2022-03-02T08:35:29","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/mixins\/"},"modified":"2023-06-26T09:11:58","modified_gmt":"2023-06-26T09:11:58","slug":"mixins","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/mixins\/","title":{"rendered":"Mixins"},"content":{"rendered":"<p>In my previous&nbsp; post &#8220;More about Dynamic and Static Polymorphism&#8221;, I used the Curiously Recurring Template Pattern (CRTP) to implement static polymorphism. Another typical use case for CRTP is mixins.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6312\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtp.png\" alt=\"StaticDynamic\" width=\"650\" height=\"401\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtp.png 1767w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtp-300x185.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtp-1024x632.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtp-768x474.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtp-1536x948.png 1536w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\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. Therefore, it&#8217;s an often-used technique in Python to change the behavior of a class by using multiple inheritances. Contrary to C++, it is legal in Python to have more than one method definition in a class hierarchy. Python uses that method that is first in the <a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-method-resolution-order\">Method Resolution Order <\/a>(MRO).<\/p>\n<p>You can implement mixins in C++ by using CRTP. A prominent example is the class<code> std::enable_shared_from_this<\/code>.<\/p>\n<h2><code>std::enable_shared_from_this<\/code><\/h2>\n<p>Let&#8217;s see <code>std::enable_shared_from<\/code> this and, therefore, CRTP applied.<\/p>\n<p>&nbsp;<\/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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ enableShared.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: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">ShareMe<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> std<span style=\"color: #555555;\">::<\/span>enable_shared_from_this<span style=\"color: #555555;\">&lt;<\/span>ShareMe<span style=\"color: #555555;\">&gt; <\/span>{  <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>ShareMe<span style=\"color: #555555;\">&gt;<\/span> getShared(){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> shared_from_this();                                <em><span style=\"color: #0099ff;\">  \/\/ (2)<\/span><\/em>\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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>ShareMe<span style=\"color: #555555;\">&gt;<\/span> shareMe(<span style=\"color: #006699; font-weight: bold;\">new<\/span> ShareMe);\r\n  std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>ShareMe<span style=\"color: #555555;\">&gt;<\/span> shareMe1<span style=\"color: #555555;\">=<\/span> shareMe<span style=\"color: #555555;\">-&gt;<\/span>getShared();     <em><span style=\"color: #0099ff;\"> \/\/ (3)<\/span><\/em>\r\n  {\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> shareMe2(shareMe1);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"shareMe.use_count(): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> shareMe.use_count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  }\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"shareMe.use_count(): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> shareMe.use_count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n  shareMe1.reset();\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"shareMe.use_count(): \"<\/span>  <span style=\"color: #555555;\">&lt;&lt;<\/span> shareMe.use_count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>By using the class, <code>std::enable_shared_from_this<\/code> you can create objects that return a&nbsp; <code>std::shared_ptr<\/code> to itself. You must derive your class public from (line 1) to get it. Now, your class <code>MySharedClass<\/code> has a member function<code> shared_from_this<\/code> (line 2) for creating <code>std::shared_ptr<\/code> to its objects. The call <code>shareMe-&gt;getShared()<\/code> (line 3) creates a new smart pointer. The member function <code>getShared<\/code> internally uses the function <code>shared_from_this<\/code> (line 2). The following screenshot shows the creation of the shared pointer.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5068\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/enabledShared.png\" alt=\"enabledShared\" width=\"397\" height=\"208\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/enabledShared.png 397w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/enabledShared-300x157.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>To learn more about smart pointers, read my previous posts:<a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/smart-pointers\"> smart pointers<\/a>.<\/p>\n<p>How do mixin classes work in C++? Let me introduce a typical use case.<\/p>\n<\/p>\n<h2>Extending a Class with all Relational Operators<\/h2>\n<p>Imagine you want to implement all six comparison operators for your data type.&nbsp; (Of course, with C++20, the compiler can auto-generate them:&nbsp; <a href=\"https:\/\/www.modernescpp.com\/C++20:%20The%20Three-Way%20Comparison%20Operator\">C++20: The Three-Way Comparison Operator<\/a>). So far, you have only implemented the smaller operator (&lt;). You know that you can derive all other five comparison operators (<code>&lt;=, &gt;, &gt;=, ==<\/code> , and<code> !=<\/code>) from the smaller operator. Applying this idea and using CRTP save you many keyboard strokes.<\/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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ mixins.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&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> Derived<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Relational {\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">&gt;<\/span> (Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op1, Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op2){\r\n       <span style=\"color: #006699; font-weight: bold;\">return<\/span> op2 <span style=\"color: #555555;\">&lt;<\/span> op1;\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">==<\/span> (Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op1, Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op2){\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">!<\/span>(op1 <span style=\"color: #555555;\">&lt;<\/span> op2) <span style=\"color: #555555;\">&amp;&amp;<\/span> <span style=\"color: #555555;\">!<\/span>(op2 <span style=\"color: #555555;\">&lt;<\/span> op1);\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">!=<\/span> (Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op1, Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op2){\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> (op1 <span style=\"color: #555555;\">&lt;<\/span> op2) <span style=\"color: #555555;\">||<\/span> (op2 <span style=\"color: #555555;\">&lt;<\/span> op1);\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">&lt;=<\/span> (Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op1, Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op2){ \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> (op1 <span style=\"color: #555555;\">&lt;<\/span> op2) <span style=\"color: #555555;\">||<\/span> (op1 <span style=\"color: #555555;\">==<\/span> op2);\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">&gt;=<\/span> (Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op1, Derived <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> op2){\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> (op1 <span style=\"color: #555555;\">&gt;<\/span> op2) <span style=\"color: #555555;\">||<\/span> (op1 <span style=\"color: #555555;\">==<\/span> op2);\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Apple<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Relational<span style=\"color: #555555;\">&lt;<\/span>Apple<span style=\"color: #555555;\">&gt;<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Apple(<span style=\"color: #007788; font-weight: bold;\">int<\/span> s)<span style=\"color: #555555;\">:<\/span> size{s}{};\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">&lt;<\/span> (Apple <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> a1, Apple <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> a2){    <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> a1.size <span style=\"color: #555555;\">&lt;<\/span> a2.size;\r\n    }\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> size;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Man<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Relational<span style=\"color: #555555;\">&lt;<\/span>Man<span style=\"color: #555555;\">&gt;<\/span>{                                <em><span style=\"color: #0099ff;\"> \/\/ (3)<\/span><\/em>\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> Man(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> n)<span style=\"color: #555555;\">:<\/span> name{n}{}\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span> <span style=\"color: #555555;\">&lt;<\/span> (Man <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> m1, Man <span style=\"color: #006699; font-weight: bold;\">const<\/span><span style=\"color: #555555;\">&amp;<\/span> m2){         <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> m1.name <span style=\"color: #555555;\">&lt;<\/span> m2.name;\r\n    }\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>string name;\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>boolalpha <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n  Apple apple1{<span style=\"color: #ff6600;\">5<\/span>};\r\n  Apple apple2{<span style=\"color: #ff6600;\">10<\/span>}; \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"apple1 &lt; apple2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (apple1 <span style=\"color: #555555;\">&lt;<\/span> apple2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"apple1 &gt; apple2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (apple1 <span style=\"color: #555555;\">&gt;<\/span> apple2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"apple1 == apple2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (apple1 <span style=\"color: #555555;\">==<\/span> apple2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"apple1 != apple2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (apple1 <span style=\"color: #555555;\">!=<\/span> apple2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"apple1 &lt;= apple2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (apple1 <span style=\"color: #555555;\">&lt;=<\/span> apple2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"apple1 &gt;= apple2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (apple1 <span style=\"color: #555555;\">&gt;=<\/span> apple2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n  Man man1{<span style=\"color: #cc3300;\">\"grimm\"<\/span>};\r\n  Man man2{<span style=\"color: #cc3300;\">\"jaud\"<\/span>};\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 &lt; man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">&lt;<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 &gt; man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">&gt;<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 == man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">==<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 != man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">!=<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 &lt;= man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">&lt;=<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 &gt;= man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">&gt;=<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I&#8217;ve for <code>Apple<\/code> and <code>Man<\/code> implemented the smaller operator (lines 1 and 2).&nbsp; For simplicity, I only use the class <code>Man<\/code> in my argumentation.&nbsp; Man is <code>public<\/code> derived from the class <code>Relational&lt;Man&gt;<\/code> (line 3) using CRTP. The class <code>Relational<\/code> supports the five missing relational operators (<code>&lt;=, &lt;,&gt;=, ==<\/code>, and<code> !=<\/code>).&nbsp; The five relations operators are mapped onto the less operator of Man (line 2).&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6313\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/mixins.png\" alt=\"mixins\" width=\"401\" height=\"462\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/mixins.png 401w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/mixins-260x300.png 260w\" sizes=\"auto, (max-width: 401px) 100vw, 401px\" \/><\/p>\n<p>Honestly, I like the name mixins for this idiom. The class <code>Relational<\/code> mixes the remaining relational operators into the class <code>Man<\/code>.<\/p>\n<p>The characteristic of CRTP is that 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;\">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>      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n{\r\n    ...\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Derivedwrong<\/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> <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n{\r\n    ...\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>How can you ensure that you are not erroneously derived the wrong class <code>DervivedWrong <\/code>from <code>Base&lt;Derived&gt;<\/code> such as in line 2?<\/p>\n<h2>Checked CRTP<\/h2>\n<p>The trick is straightforward: make the constructor of <code>Base<\/code> private.<\/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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ crtpCheck.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> Derived<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Base{\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> interface(){\r\n    <span style=\"color: #006699; font-weight: bold;\">static_cast<\/span><span style=\"color: #555555;\">&lt;<\/span>Derived<span style=\"color: #555555;\">*&gt;<\/span>(<span style=\"color: #006699; font-weight: bold;\">this<\/span>)<span style=\"color: #555555;\">-&gt;<\/span>implementation();\r\n  }\r\n<span style=\"color: #9999ff;\">private:<\/span>\r\n    Base() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;            <em> <span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n    <span style=\"color: #006699; font-weight: bold;\">friend<\/span> Derived;             <em><span style=\"color: #0099ff;\">  \/\/ (2)<\/span><\/em>\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Derived1<span style=\"color: #555555;\">:<\/span> Base<span style=\"color: #555555;\">&lt;<\/span>Derived1<span style=\"color: #555555;\">&gt;<\/span>{\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> implementation(){\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Implementation Derived1\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Derived2<span style=\"color: #555555;\">:<\/span> Base<span style=\"color: #555555;\">&lt;<\/span>Derived1<span style=\"color: #555555;\">&gt;<\/span>{   <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em>\r\n  <span style=\"color: #007788; font-weight: bold;\">void<\/span> implementation(){\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Implementation Derived1\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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: #007788; font-weight: bold;\">void<\/span> execute(T<span style=\"color: #555555;\">&amp;<\/span> base){\r\n    base.interface();\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n  Derived1 d1;\r\n  execute(d1);\r\n    \r\n  Derived2 d2;\r\n  execute(d2);\r\n  \r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><code>Base<\/code> has a private default constructor (line 1). Only the class <code>Base<\/code> itself or the friend <code>Derived<\/code> (line 2) can invoke the default constructor. Consequentially, the call<code> Derived2 d2<\/code> (line 3) fails because<code> Derived2<\/code> it is derived from <code>Base&lt;Derived1&gt;<\/code>. Here is the error message from GCC:<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6314\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtpCheck.png\" alt=\"crtpCheck\" width=\"650\" height=\"201\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtpCheck.png 1202w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtpCheck-300x93.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtpCheck-1024x317.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/03\/crtpCheck-768x238.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/h2>\n<h2>What&#8217;s next?<\/h2>\n<p>The Curiously Recurring Template Pattern (CRTP) is not easy to understand but very powerful. The same holds for expression templates. Expression templates allow you to get rid of superfluous temporaries.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous&nbsp; post &#8220;More about Dynamic and Static Polymorphism&#8221;, I used the Curiously Recurring Template Pattern (CRTP) to implement static polymorphism. Another typical use case for CRTP is mixins.<\/p>\n","protected":false},"author":21,"featured_media":6312,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[426,425],"class_list":["post-6315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-crtp","tag-mixins"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6315","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=6315"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6315\/revisions"}],"predecessor-version":[{"id":6679,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6315\/revisions\/6679"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6312"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}