    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Letter to the Editor</title>
        <link>https://members.accu.org/index.php/journals/1876</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #113 - February 2013 + Letters to the Editor</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c320/">o113</a>
                    (7)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c186/">LettersEditor</a>
                    (132)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c320-186/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c320+186/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Letter to the Editor</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 01 February 2013 21:02:06 +00:00 or Fri, 01 February 2013 21:02:06 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<p>Dear Editor,</p>

<p>I really enjoyed Cassio Neriâ€™s article on â€˜Complex Logic in the Member Initialiser Listâ€™ as itâ€™s a problem Iâ€™ve had to deal with several times. I was surprised to learn that Listing 6 was valid, because I didnâ€™t realise callers of that constructor didnâ€™t need access to the private â€˜storageâ€™ type to create the default argument at the call site. On reflection I realised the caller doesnâ€™t have to name the type and that access checking in C++ is done on names. I love an <em>Overload</em> article that makes me stop halfway through reading to reach for the compiler and learn something new!</p>

<p>Despite discussing how some C++11 features help solve the problems being discussed, I was surprised to notice the article didnâ€™t mention one of the new C++11 features that I find very useful for solving exactly those sort of problems. The â€˜delegating constructorsâ€™ feature allows one constructor to invoke a different constructor, in order to avoid duplicating logic in constructor bodies. This feature is useful when one constructor (the delegating constructor) wants to process or munge its arguments somehow and then pass them on to another constructor (the target constructor.) Using delegating constructors allows Cassioâ€™s Listing 6 to be rewritten like so:</p>

<pre class="programlisting">
class bar : public base {
  struct storage {
    storage(double d) : b(d * d) { }
    double b;
  };
  ...
  bar(double d, foo&amp; r1, foo&amp; r2, storage tmp);
  
public:
  bar(double d, foo&amp; r1, foo&amp; r2);
};

bar::bar(double d, foo&amp; r1, foo&amp; r2)
: bar(d, r1, r2, storage(d))
{ }

bar::bar(double d, foo&amp; r1, foo&amp; r2, storage tmp)
: base(tmp.b),
  x_(cos(tmp.b)), y_(sin(tmp.b)), ...
{ }</pre>

<p>In this version of the code the user-accessible constructor doesnâ€™t mention the private â€˜storageâ€™ type, which separates the interface meant for users from the implementation details of the complex constructor logic. Additionally, I believe it solves several of the problems mentioned in the article and makes the techniques shown in Listing 7 and Listing 8 unnecessary, and ultimately avoids the need to use a discriminated union for this scenario.</p>

<p>Because the temporary â€˜storageâ€™ object is initialized in the member initializer list, not the parameter list, there is no restriction on referring to the function parameters, so â€˜storageâ€™ can be constructed with â€˜dâ€™ and can have an arbitrarily complex constructor that can do any necessary calculations. Because the delegating constructor doesnâ€™t initialize any base classes (thatâ€™s done by the target constructor) the â€˜storageâ€™ object is guaranteed to be initialized before the target constructor is invoked so it avoids any problems with order of initialization of base classes.</p>

<p>I think using delegating constructors for this problem is almost the ideal solution. The â€˜storageâ€™ constructor allows the complex logic to be placed in a separate function where it can be written more naturally (rather than in a contrived function such as <code>bar::init_base</code>) and that function is guaranteed to be executed at exactly the right time: after the user calls the (delegating) constructor but before the invocation of the target constructor that actually performs initialization of the base classes and members.</p>

<p>The only downside, which might be why they werenâ€™t considered in the original article, is that delegating constructors were not widely supported until quite recently. Clang has supported them since version 3.0, GCC since 4.7 and MSVC now supports them too as of the November 2012 CTP.</p>

<p>Yours,</p>
<p>Jonathan Wakely</p>

<p class="blockquote">Dear Jonathan,</p>
<p class="blockquote">Thank you for bringing this to our attention. Feel free to write in with any further comments. Cassio tells me Jeff Snyder (who can be found online as je4d) contacted him with similar remarks about delegating constructors.</p>
<p class="blockquote">Frances â€“ Overload editor</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
