<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://www.baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2015:03:03</title>
        <description></description>
        <link>https://www.baszerr.eu/</link>
        <lastBuildDate>Wed, 06 May 2026 09:37:48 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://www.baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://www.baszerr.eu/</link>
        </image>
        <item>
            <title>init_via_lambda</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2015:03:03:init_via_lambda</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;init_via_lambda&quot;&gt;2015-03-03 - init via lambda&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
&lt;a href=&quot;https://www.baszerr.eu/lib/exe/detail.php?id=blog%3A2015%3A03%3A03%3Ainit_via_lambda&amp;amp;media=blog:2015:03:03:lambda_init.png&quot; class=&quot;media&quot; title=&quot;blog:2015:03:03:lambda_init.png&quot;&gt;&lt;img src=&quot;https://www.baszerr.eu/lib/exe/fetch.php?media=blog:2015:03:03:lambda_init.png&quot; class=&quot;mediaright&quot; align=&quot;right&quot; loading=&quot;lazy&quot; title=&quot;lambda init&quot; alt=&quot;lambda init&quot; /&gt;&lt;/a&gt;
today at work i was refactoring simple piece of code – a c-tor:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Sth
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  SomeClass&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;:&lt;/span&gt;
    name_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;sth&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;,
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    factory_.&lt;span class=&quot;me1&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;, builderFunction&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt; name_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Factory     factory_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
Sth sth&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
Producer p&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;sth.&lt;span class=&quot;me1&quot;&gt;name_&lt;/span&gt;, sth.&lt;span class=&quot;me1&quot;&gt;factory_&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
but due to different separation of concerns, after changes in other locations, i was in a need to put &lt;em&gt;Producer&lt;/em&gt; into &lt;em&gt;Sth&lt;/em&gt; as well:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Sth
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  SomeClass&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;:&lt;/span&gt;
    name_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;sth&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;,
    producer_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;name_, factory_&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;  &lt;span class=&quot;co1&quot;&gt;// Oops - factory_ is not yet initialized!&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    factory_.&lt;span class=&quot;me1&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;, builderFunction&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt; name_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Factory     factory_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Producer    producer_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
the problem was order – i needed to initialize &lt;em&gt;factory_&lt;/em&gt; first (this has been done in a c-tor body so far) and then initialize &lt;em&gt;producer_&lt;/em&gt;. the obvious solution was to add a helper function, that would return properly-defined factory, and then call it from my c-tor, to initialize class&amp;#039; member:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;Factory createFactory&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  Factory f&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  f.&lt;span class=&quot;me1&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;, builderFunction&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; f&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Sth
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  SomeClass&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;:&lt;/span&gt;
    name_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;sth&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;,
    factory_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; createFactory&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;,
    producer_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;name_, factory_&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;   &lt;span class=&quot;co1&quot;&gt;// ok - factory_ is now properly initialized&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt; name_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Factory     factory_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Producer    producer_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
this is however a bit non-local. either i had to create function, in a unnamed namespace, inside my implementation, or &lt;em&gt;detail&lt;/em&gt; namespace if this is all in header,
or create private, static member function to do this work for me.
&lt;/p&gt;

&lt;p&gt;
can this be done better? using &lt;a href=&quot;https://en.wikipedia.org/wiki/C++14&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++14&quot;&gt;C++14&lt;/a&gt; (and &lt;a href=&quot;https://en.wikipedia.org/wiki/C++11&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++11&quot;&gt;C++11&lt;/a&gt;, with a bit more code) it can. the idea is to provide a lambda, that would do the initialization and call it in place:
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Sth
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  SomeClass&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;:&lt;/span&gt;
    name_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;sth&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;,
    factory_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; Factory f&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; f.&lt;span class=&quot;me1&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;type&amp;quot;&lt;/span&gt;, builderFunction&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; f&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span class=&quot;co1&quot;&gt;// note the ending &#039;()&#039;!&lt;/span&gt;
    producer_&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;name_, factory_&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;   &lt;span class=&quot;co1&quot;&gt;// ok - factory_ is now properly initialized&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  std&lt;span class=&quot;sy4&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;string&lt;/span&gt; name_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Factory     factory_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  Producer    producer_&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
this way initialization is done in-place, using local code only. more over, there is a good change compiler will catch your drift and simply insert expected code inline, w/o a need to do any function-calling, objects moving, etc…
&lt;/p&gt;

&lt;p&gt;
honestly this pattern is trivial. it is trivial to the point i was surprised i have not used it until now… ;)
&lt;/p&gt;

&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:09:30 +0000</pubDate>
        </item>
    </channel>
</rss>
