{"id":7642,"date":"2023-07-17T15:53:09","date_gmt":"2023-07-17T15:53:09","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=7642"},"modified":"2023-08-11T15:55:54","modified_gmt":"2023-08-11T15:55:54","slug":"c23-deducing-this","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c23-deducing-this\/","title":{"rendered":"C++23: Deducing This"},"content":{"rendered":"\n<p>Anyone who thinks a small C++ standard follows a significant C++ standard is wrong. C++23 provides powerful extensions to C++20. These extensions include the core language, particularly the standard library. Today, I present a small but very impactful feature of the core language: deducing this.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"579\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/DeducingThis-1-1030x579.png\" alt=\"\" class=\"wp-image-7770\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/DeducingThis-1-1030x579.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/DeducingThis-1-300x169.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/DeducingThis-1-768x432.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/DeducingThis-1-705x397.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/DeducingThis-1.png 1280w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p>Deducing this, sometimes also called explicit object parameter, allows it to make the implicit <code>this<\/code>  pointer of a member function explicit. Like Python, the explicit object  parameter must be the first function parameter and is called in C++, by  convention, <code>Self<\/code> and <code>self<\/code>.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">struct<\/span> Test {\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> implicitParameter();               <span style=\"color: #0099FF; font-style: italic\">\/\/ implicit this pointer<\/span>\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">explictParameter<\/span>(<span style=\"color: #006699; font-weight: bold\">this<\/span> Self<span style=\"color: #555555\">&amp;<\/span> self); <span style=\"color: #0099FF; font-style: italic\">\/\/ explicit this pointer<\/span>\n};\n<\/pre><\/div>\n\n\n\n<p>Deducing this enables new programming techniques in C++23: deduplication of function overloading based on the object&#8217;s lvalue\/rvalue value category and constness. Additionally, you can reference a lambda and invoke it recursively. Furthermore, deducing this simplifies the implementation of <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-is-still-lazy\">CRTP<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h1-deduplicating-function-overloading\">Deduplicating Function Overloading<\/h2>\n\n\n\n<p>Assume you want to overload a member function based on the lvalue\/rvalue value category and constness of the calling object. This means you have to overload your member function four times.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ deducingThis.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">struct<\/span> Test {\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> Self<span style=\"color: #555555\">&gt;<\/span>\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> explicitCall(<span style=\"color: #006699; font-weight: bold\">this<\/span> Self<span style=\"color: #555555\">&amp;&amp;<\/span> self, <span style=\"color: #006699; font-weight: bold\">const<\/span> std<span style=\"color: #555555\">::<\/span>string<span style=\"color: #555555\">&amp;<\/span> text) {  <span style=\"color: #0099FF; font-style: italic\">\/\/ (9)<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> text <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;: &quot;<\/span>;\n        std<span style=\"color: #555555\">::<\/span>forward<span style=\"color: #555555\">&lt;<\/span>Self<span style=\"color: #555555\">&gt;<\/span>(self).implicitCall();                    <span style=\"color: #0099FF; font-style: italic\">\/\/ (10)<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> implicitCall() <span style=\"color: #555555\">&amp;<\/span> {                          <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;non const lvalue&quot;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> implicitCall() <span style=\"color: #006699; font-weight: bold\">const<\/span><span style=\"color: #555555\">&amp;<\/span> {                     <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;const lvalue&quot;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> implicitCall() <span style=\"color: #555555\">&amp;&amp;<\/span> {                         <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;non const rvalue&quot;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> implicitCall() <span style=\"color: #006699; font-weight: bold\">const<\/span><span style=\"color: #555555\">&amp;&amp;<\/span> {                    <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;const rvalue&quot;<\/span>;\n    }\n\n};\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    Test test;\n    <span style=\"color: #006699; font-weight: bold\">const<\/span> Test constTest;\n\n    test.explicitCall(<span style=\"color: #CC3300\">&quot;test&quot;<\/span>);                                  <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/span>\n    constTest.explicitCall(<span style=\"color: #CC3300\">&quot;constTest&quot;<\/span>);                        <span style=\"color: #0099FF; font-style: italic\">\/\/ (6)<\/span>\n    std<span style=\"color: #555555\">::<\/span>move(test).explicitCall(<span style=\"color: #CC3300\">&quot;std::move(test)&quot;<\/span>);            <span style=\"color: #0099FF; font-style: italic\">\/\/ (7)<\/span>\n    std<span style=\"color: #555555\">::<\/span>move(constTest).explicitCall(<span style=\"color: #CC3300\">&quot;std::move(consTest)&quot;<\/span>);   <span style=\"color: #0099FF; font-style: italic\">\/\/ (8)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>Lines (1), (2), (3), and (4) are the required function overloads. Lines (1) and (2) take a non-const and const lvalue object, and lines (3) and (4) a non-const and const rvalue objects.&nbsp;To simplify, a lvalue is a value from which the address can be determined, and a rvalue is an only readable value. The lines (5) to (8) represent the corresponding objects. Deducing this in line (9) enables it to deduplicate the four overloads in one member function that perfectly forwards <code>self<\/code> (line 10) and calls <code>implicitCall<\/code>. This article goes into the intricacies of perfect forwarding: <a href=\"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\">perfect forwarding<\/a>.<\/p>\n\n\n\n<p>Currently (July 2023), neither the brand new GCC nor Clang C++ Compiler supports this feature, but the Microsoft Visual C++ compiler partially. The following screenshot shows that the four function calls in the main function use the four different overloads of the member function <code>implicitCall<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"493\" height=\"248\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/deducingThisCRTP.png\" alt=\"\" class=\"wp-image-7777\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/deducingThisCRTP.png 493w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/deducingThisCRTP-300x151.png 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/figure>\n\n\n\n<p>As I promised, deducing this is a pretty small future, but &#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h2-reference-a-lambda\">Reference a Lambda<\/h2>\n\n\n\n<p>The crucial idea of the <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-visitor-pattern\">Visitor Pattern<\/a> is to perform operations on an object hierarchy. The object hierarchy is stable in this classical pattern, but the operations may change frequently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h2-1-the-visitor-pattern\">The Visitor Pattern<\/h3>\n\n\n\n<p>The following program <code>visitor.cpp<\/code> exemplifies this pattern.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ visitor.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;vector&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">CarElementVisitor<\/span>;\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">CarElement<\/span> {                                     <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/span>\n <span style=\"color: #9999FF\">public:<\/span>\n    <span style=\"color: #006699; font-weight: bold\">virtual<\/span> <span style=\"color: #007788; font-weight: bold\">void<\/span> accept(CarElementVisitor<span style=\"color: #555555\">&amp;<\/span> visitor) <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">virtual<\/span> <span style=\"color: #555555\">~<\/span>CarElement() <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">default<\/span>;\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Body<\/span>;\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Car<\/span>;\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Engine<\/span>;\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Wheel<\/span>;\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">CarElementVisitor<\/span> {                              <span style=\"color: #0099FF; font-style: italic\">\/\/ (6)<\/span>\n <span style=\"color: #9999FF\">public:<\/span>\n    <span style=\"color: #006699; font-weight: bold\">virtual<\/span> <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Body body) <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">virtual<\/span> <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Car car) <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">virtual<\/span> <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Engine engine) <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">virtual<\/span> <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Wheel wheel) <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">virtual<\/span> <span style=\"color: #555555\">~<\/span>CarElementVisitor() <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">default<\/span>;\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Wheel<\/span><span style=\"color: #555555\">:<\/span> <span style=\"color: #006699; font-weight: bold\">public<\/span> CarElement {\n <span style=\"color: #9999FF\">public:<\/span>\n    Wheel(<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) { }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> accept(CarElementVisitor<span style=\"color: #555555\">&amp;<\/span> visitor) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        visitor.visit(<span style=\"color: #555555\">*<\/span><span style=\"color: #006699; font-weight: bold\">this<\/span>);\n    }\n\n    std<span style=\"color: #555555\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold\">const<\/span> {\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> name;\n    }\n <span style=\"color: #9999FF\">private:<\/span>\n    std<span style=\"color: #555555\">::<\/span>string name;\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Body<\/span><span style=\"color: #555555\">:<\/span> <span style=\"color: #006699; font-weight: bold\">public<\/span> CarElement {\n <span style=\"color: #9999FF\">public:<\/span>\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> accept(CarElementVisitor<span style=\"color: #555555\">&amp;<\/span> visitor) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        visitor.visit(<span style=\"color: #555555\">*<\/span><span style=\"color: #006699; font-weight: bold\">this<\/span>);\n    }\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Engine<\/span><span style=\"color: #555555\">:<\/span> <span style=\"color: #006699; font-weight: bold\">public<\/span> CarElement {\n <span style=\"color: #9999FF\">public:<\/span>\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> accept(CarElementVisitor<span style=\"color: #555555\">&amp;<\/span> visitor) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        visitor.visit(<span style=\"color: #555555\">*<\/span><span style=\"color: #006699; font-weight: bold\">this<\/span>);\n    }\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Car<\/span><span style=\"color: #555555\">:<\/span> <span style=\"color: #006699; font-weight: bold\">public<\/span> CarElement {\n <span style=\"color: #9999FF\">public:<\/span>\n    Car(std<span style=\"color: #555555\">::<\/span>initializer_list<span style=\"color: #555555\">&lt;<\/span>CarElement<span style=\"color: #555555\">*&gt;<\/span> carElements )<span style=\"color: #555555\">:<\/span>\n      elements{carElements} {}\n   \n    <span style=\"color: #007788; font-weight: bold\">void<\/span> accept(CarElementVisitor<span style=\"color: #555555\">&amp;<\/span> visitor) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> elem <span style=\"color: #555555\">:<\/span> elements) {\n            elem<span style=\"color: #555555\">-&gt;<\/span>accept(visitor);\n        }\n        visitor.visit(<span style=\"color: #555555\">*<\/span><span style=\"color: #006699; font-weight: bold\">this<\/span>);\n    }\n <span style=\"color: #9999FF\">private:<\/span>\n    std<span style=\"color: #555555\">::<\/span>vector<span style=\"color: #555555\">&lt;<\/span>CarElement<span style=\"color: #555555\">*&gt;<\/span> elements;                   <span style=\"color: #0099FF; font-style: italic\">\/\/ (7)<\/span>\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">CarElementDoVisitor<\/span><span style=\"color: #555555\">:<\/span> <span style=\"color: #006699; font-weight: bold\">public<\/span> CarElementVisitor {\n   \n    <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Body body) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Moving my body&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n     <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Car car) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Starting my car&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Wheel wheel) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Kicking my &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> wheel.getName() \n          <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; wheel&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Engine engine) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Starting my engine&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">CarElementPrintVisitor<\/span><span style=\"color: #555555\">:<\/span> <span style=\"color: #006699; font-weight: bold\">public<\/span> CarElementVisitor {\n   \n    <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Body body) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting body&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n     <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Car car) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting car&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Wheel wheel) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> wheel.getName() \n          <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; wheel&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> visit(Engine engine) <span style=\"color: #006699; font-weight: bold\">const<\/span> override {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting engine&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n};\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    Wheel wheelFrontLeft(<span style=\"color: #CC3300\">&quot;front left&quot;<\/span>);        \n    Wheel wheelFrontRight(<span style=\"color: #CC3300\">&quot;front right&quot;<\/span>);\n    Wheel wheelBackLeft(<span style=\"color: #CC3300\">&quot;back left&quot;<\/span>);\n    Wheel wheelBackRight(<span style=\"color: #CC3300\">&quot;back right&quot;<\/span>);\n    Body body;\n    Engine engine;\n    Car car {<span style=\"color: #555555\">&amp;<\/span>wheelFrontLeft, <span style=\"color: #555555\">&amp;<\/span>wheelFrontRight, \n             <span style=\"color: #555555\">&amp;<\/span>wheelBackLeft, <span style=\"color: #555555\">&amp;<\/span>wheelBackRight,\n             <span style=\"color: #555555\">&amp;<\/span>body, <span style=\"color: #555555\">&amp;<\/span>engine};\n\n    CarElementPrintVisitor carElementPrintVisitor; \n\n    engine.accept(carElementPrintVisitor);       <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)      <\/span>\n    car.accept(carElementPrintVisitor);          <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    CarElementDoVisitor carElementDoVisitor;\n\n    engine.accept(carElementDoVisitor);          <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    car.accept(carElementDoVisitor);             <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>All the <code>car<\/code>&#8216;s components are created at the beginning of the main function. After that, the <code>engine<\/code> and the <code>car<\/code> take the <code>carElementPrintVisitor<\/code> (1 and 2). In lines (3) and (4), both objects are accepted by <code>carElementDoVisitor<\/code>. <code>CarElement<\/code> (5) and <code>CarElementVisitor<\/code> (6) are the abstract base classes of the object and operation hierarchies. The <code>car<\/code> is the most interesting component because it holds its components in a <code>std::vector&lt;element*&gt;<\/code> (7). The crucial observation of the visitor pattern is that it depends on two objects, which operation is performed: the visitor and the visited object.<\/p>\n\n\n\n<p>Here is the output of the program:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"537\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/visitor.png\" alt=\"\" class=\"wp-image-7645\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/visitor.png 367w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/visitor-205x300.png 205w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n\n\n\n<p>You will find more information on the Visitor Pattern in my previous post: <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-visitor-pattern\">The Visitor Pattern<\/a>. Admittedly, the Visitor Pattern is very resistant to understanding. Thanks to the overload pattern, this changes with C++23.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h2-2-the-overload-pattern\">The Overload Pattern<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.modernescpp.com\/index.php\/visiting-a-std-variant-with-the-overload-pattern\">Overload Pattern<\/a> is the modern C++ version of the Visitor Pattern. It combines variadic templates with <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/variant\">std:variant <\/a><\/code>and its function <code>std::visit<\/code>.<\/p>\n\n\n\n<p>Thanks to deducing this in C++23, a lambda expression can explicitly refer to its implicit lambda object.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ visitOverload.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;vector&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;variant&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span>class... Ts<span style=\"color: #555555\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold\">struct<\/span> overloaded <span style=\"color: #555555\">:<\/span> Ts... { \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>()...; \n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Wheel<\/span> {\n <span style=\"color: #9999FF\">public:<\/span>\n    Wheel(<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) { }\n    std<span style=\"color: #555555\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold\">const<\/span> {\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> name;\n    }\n <span style=\"color: #9999FF\">private:<\/span>\n    std<span style=\"color: #555555\">::<\/span>string name;\n};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Body<\/span> {};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Engine<\/span> {};\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Car<\/span>;\n\n<span style=\"color: #006699; font-weight: bold\">using<\/span> CarElement <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>variant<span style=\"color: #555555\">&lt;<\/span>Wheel, Body, Engine, Car<span style=\"color: #555555\">&gt;<\/span>;\n\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">Car<\/span> {\n <span style=\"color: #9999FF\">public:<\/span>\n    Car(std<span style=\"color: #555555\">::<\/span>initializer_list<span style=\"color: #555555\">&lt;<\/span>CarElement<span style=\"color: #555555\">*&gt;<\/span> carElements )<span style=\"color: #555555\">:<\/span>\n      elements{carElements} {}\n   \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> \n   <span style=\"color: #007788; font-weight: bold\">void<\/span> visitCarElements(T<span style=\"color: #555555\">&amp;&amp;<\/span> visitor) <span style=\"color: #006699; font-weight: bold\">const<\/span> {\n       <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> elem <span style=\"color: #555555\">:<\/span> elements) {\n           std<span style=\"color: #555555\">::<\/span>visit(visitor, <span style=\"color: #555555\">*<\/span>elem);\n       }\n   }\n <span style=\"color: #9999FF\">private:<\/span>\n    std<span style=\"color: #555555\">::<\/span>vector<span style=\"color: #555555\">&lt;<\/span>CarElement<span style=\"color: #555555\">*&gt;<\/span> elements;\n};\n\noverloaded carElementPrintVisitor {                                            <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n    [](<span style=\"color: #006699; font-weight: bold\">const<\/span> Body<span style=\"color: #555555\">&amp;<\/span> body)     {  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting body&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; },      \n    [](<span style=\"color: #006699; font-weight: bold\">this<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> <span style=\"color: #006699; font-weight: bold\">const<\/span><span style=\"color: #555555\">&amp;<\/span> self, <span style=\"color: #006699; font-weight: bold\">const<\/span> Car<span style=\"color: #555555\">&amp;<\/span> car)  {  car.visitCarElements(self);  <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n                                                  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting car&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; },\n    [](<span style=\"color: #006699; font-weight: bold\">const<\/span> Wheel<span style=\"color: #555555\">&amp;<\/span> wheel)   {  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting &quot;<\/span> \n                                          <span style=\"color: #555555\">&lt;&lt;<\/span> wheel.getName() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; wheel&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; },\n    [](<span style=\"color: #006699; font-weight: bold\">const<\/span> Engine<span style=\"color: #555555\">&amp;<\/span> engine) {  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Visiting engine&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;}\n};\n\noverloaded carElementDoVisitor {                                               <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    [](<span style=\"color: #006699; font-weight: bold\">const<\/span> Body<span style=\"color: #555555\">&amp;<\/span> body)     {  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Moving my body&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; },\n    [](<span style=\"color: #006699; font-weight: bold\">this<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> <span style=\"color: #006699; font-weight: bold\">const<\/span><span style=\"color: #555555\">&amp;<\/span> self, <span style=\"color: #006699; font-weight: bold\">const<\/span> Car<span style=\"color: #555555\">&amp;<\/span> car) {  car.visitCarElements(self);   <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/span>\n                                                std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Starting my car&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; },\n    [](<span style=\"color: #006699; font-weight: bold\">const<\/span> Wheel<span style=\"color: #555555\">&amp;<\/span> wheel)   {  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Kicking my &quot;<\/span> \n                                          <span style=\"color: #555555\">&lt;&lt;<\/span> wheel.getName()  <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; wheel&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; },\n    [](<span style=\"color: #006699; font-weight: bold\">const<\/span> Engine<span style=\"color: #555555\">&amp;<\/span> engine) {  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Starting my engine&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;}\n};\n \n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    CarElement wheelFrontLeft  <span style=\"color: #555555\">=<\/span> Wheel(<span style=\"color: #CC3300\">&quot;front left&quot;<\/span>);        \n    CarElement wheelFrontRight <span style=\"color: #555555\">=<\/span> Wheel(<span style=\"color: #CC3300\">&quot;front right&quot;<\/span>);\n    CarElement wheelBackLeft   <span style=\"color: #555555\">=<\/span> Wheel(<span style=\"color: #CC3300\">&quot;back left&quot;<\/span>);\n    CarElement wheelBackRight  <span style=\"color: #555555\">=<\/span> Wheel(<span style=\"color: #CC3300\">&quot;back right&quot;<\/span>);\n    CarElement body            <span style=\"color: #555555\">=<\/span> Body{};\n    CarElement engine          <span style=\"color: #555555\">=<\/span> Engine{};\n \n    CarElement car  <span style=\"color: #555555\">=<\/span> Car{<span style=\"color: #555555\">&amp;<\/span>wheelFrontLeft, <span style=\"color: #555555\">&amp;<\/span>wheelFrontRight,                    <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n             <span style=\"color: #555555\">&amp;<\/span>wheelBackLeft, <span style=\"color: #555555\">&amp;<\/span>wheelBackRight,\n             <span style=\"color: #555555\">&amp;<\/span>body, <span style=\"color: #555555\">&amp;<\/span>engine};\n\n    std<span style=\"color: #555555\">::<\/span>visit(carElementPrintVisitor, engine);\n    std<span style=\"color: #555555\">::<\/span>visit(carElementPrintVisitor, car);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>visit(carElementDoVisitor, engine);\n    std<span style=\"color: #555555\">::<\/span>visit(carElementDoVisitor, car);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    \n}\n<\/pre><\/div>\n\n\n\n<p>The <code>Car<\/code> (1) represents the object hierarchy, and the two operations <code>carElementPrintVisitor<\/code> (2) and <code>carElementDoVistor<\/code> (3) represent the visitors.&nbsp;The lambda expressions in lines (4) and (5) that visit the <code>car<\/code> can reference the implicit lambda object and thus visit the concrete components of the car: <code>car.visitCarElement(self)<\/code>. The output of the previous program<code> vistitor.cpp<\/code> and this program are identical.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"476\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/visitOverload.png\" alt=\"\" class=\"wp-image-7646\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/visitOverload.png 466w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/visitOverload-294x300.png 294w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/07\/visitOverload-36x36.png 36w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h3-what-s-next\">What&#8217;s Next?<\/h2>\n\n\n\n<p>The <strong>C<\/strong>uriously<strong> R<\/strong>ecurring <strong>T<\/strong>emplate <strong>P<\/strong>attern (CRTP) is a heavily used idiom in C++. It is similarly resistant to understanding as the classic design pattern visitor. Thanks to deducing this, we can remove the C and R from the abbreviation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anyone who thinks a small C++ standard follows a significant C++ standard is wrong. C++23 provides powerful extensions to C++20. These extensions include the core language, particularly the standard library. Today, I present a small but very impactful feature of the core language: deducing this. Deducing this, sometimes also called explicit object parameter, allows it [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":7643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[378],"tags":[],"class_list":["post-7642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-23"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/7642","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=7642"}],"version-history":[{"count":5,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/7642\/revisions"}],"predecessor-version":[{"id":8037,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/7642\/revisions\/8037"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/7643"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=7642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=7642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=7642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}