    <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  :: editor &lt;&lt; letters;</title>
        <link>https://members.accu.org/index.php/journals/521</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 #34 - Oct 1999 + Programming Topics + 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/c170/">34</a>
                    (11)
<br />

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<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/c170-65-186/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c170+65+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;editor &lt;&lt; letters;</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 October 1999 17:50:55 +01:00 or Tue, 26 October 1999 17:50:55 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>This is a little defensive-coding idea I have been thinking
about for a few moments and I discussed it briefly with Detlef
Vollmann at a recent conference, he suggested the temporary const
copy that may be optimised away...</p>
<p>What happened to me was that I was calling 'fn(int)' which was
changed (unbeknownst to me) to 'fn(int &amp;)'! The function
specifically wanted to change the object passed to it.</p>
<pre class="programlisting">
for( int i = 0 ; i != 10 ; ++i )
{
  fn(i) ;
}
</pre>
<p>Which was my code before and after the new library including
'fn', but after the silent change, this failed, randomly as the
User reported, at run-time. So I now need a good method to protect
myself from such silent changes.</p>
<p>What I want to achieve is that if I write a loop (for example a
for loop) such that any code in the body cannot corrupt the loop
index. One could write:</p>
<pre class="programlisting">
for( int i = 0 ; i != 10 ; ++i )
{
  const int ci(i) ;
  fn(ci) ;
}
</pre>
<p>Thus creating a new variable, initialised from the loop index,
but can be passed by reference to a function (the compiler may
optimise this temporary away?), but it would still be possible to
make a mistake and pass the original index rather than the const
version. The compiler now can warn me if someone wants to subvert
my index. Ideally what I'd like to write is:</p>
<pre class="programlisting">
for( const mutable int i = 0 ; i != 10 ; ++i )
{
  fn(i) ;
}
</pre>
<p>Now the compiler knows that the loop index is const within the
body of the loop, except that the loop construct wants mutable
access to the index.</p>
<p>According to 'The C++ Programming Language' 3rd edition, this is
syntactically ok. What I'd like changed in the standard is that
mutable could apply to all variables, not just objects of
class-scope. I would welcome your thoughts&hellip;</p>
<p><span class="emphasis"><em>Sean Corfield responds:</em></span>
This is a 'common' problem in that if a by-value function is
replaced by a pair (typically) of const &amp; and non-const &amp;
functions then any code you have that calls the function with a
non-const lvalue will 'suddenly' call the non-const version. Of
course, library vendors shouldn't pull this sort of stunt in any
way that would change the semantics of a call. In other words, if
the following code is guaranteed to work in one version of the
library, the vendor should not change this in a subsequent release
of the library:</p>
<pre class="programlisting">
T x( y );
fn( y );
assert( x == y );
</pre>
<p>What this basically shows is that adding a non-const&amp;
overload to existing code is almost always a bad idea - if you
intend to maintain the semantics (the implied 'assert' above) then
why accept a *non-const* parameter at all?</p>
<p>Yes, syntactically this is ok, but there are additional
constraints beyond the syntax - 'mutable' may only be applied to
class member data declarations.</p>
<p>The key issue here is that you want to ensure the correct
overload is chosen. A variant of the extra constant copy above that
would ensure the correct overload is chosen without introducing a
new variable is:</p>
<pre class="programlisting">
for ( int i = 0; i!= 10; ++i )
{
  const int&amp; ci = i;      // use ref instead of copy
  fn( ci );
}
</pre>
<p>Another possibility is simply to add a cast to the call:</p>
<pre class="programlisting">
for ( int i = 0; i!= 10; ++i )
{
  fn( const_cast&lt;const int&amp;&gt;( i ) );
}
</pre>
<p>But this looks ugly and doesn't work well with multiple calls
&shy; I'm just giving it as an example!</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
