    <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  :: Auto â€“ a necessary evil? (Part 2)</title>
        <link>https://members.accu.org/index.php/articles/1840</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>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #116 - August 2013</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/articles/">All</a>

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

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

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c328/">o116</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-328/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+328/">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;Auto â€“ a necessary evil? (Part 2)</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 01 August 2013 18:01:13 +01:00 or Thu, 01 August 2013 18:01:13 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Should you declare (almost) everything auto? Roger Orr considers when auto is appropriate.</p>
<p><strong>Body:</strong>&nbsp;<p class="quote">To have a right to do a thing<br /> is not at all the same as <br />to be right in doing it.</p>
<p class="quote">~ G.K.Chesterton</p>

<p>In the first article we covered the rules governing the <code>auto</code> keyword that was added to the language in C++11 (or added back, if your memory of C++ goes back far enough!)</p>

<p>It is important with a feature like <code>auto</code> not only to know the rules about what is permitted by the language â€“ and the meaning of the consequent code â€“ but also to be able to decide <em>when</em> the use of the feature is appropriate and what design forces need to be considered when taking such decisions.</p>

<p>In this article we look in more detail at some uses of <code>auto</code> with the intent of identifying some of these issues.</p>

<h2>A â€˜complex typeâ€™ example</h2>

<p>One of the main motivations for <code>auto</code> was to simplify the declaration of variables with â€˜complicatedâ€™ types. One such example is in the use of iterators over standard library containers in cases such as:</p>

<pre class="programlisting">
  std::vector&lt;std::set&lt;int&gt;&gt; setcoll;
  std::vector&lt;std::set&lt;int&gt;&gt;::const_iterator it =
     setcoll.cbegin();<a href="#FN01"><sup>1</sup></a>
</pre>

<p>Many programmers were put off using the STL because of the verbosity of the variable declarations. With C++03 one recommendation was to use a <code>typedef</code> â€“ and this approach remains valid in C++11:</p>

<pre class="programlisting">
  typedef std::vector&lt;std::set&lt;int&gt; &gt; collType;
  // C++03 code still works fine
  collType setcoll;6&quot;&gt;
  collType::const_iterator it = setcoll.begin();
</pre>

<p>With the addition of <code>auto</code> to the language the code can be shortened considerably:</p>

<pre class="programlisting">
  std::vector&lt;std::set&lt;int&gt;&gt; setcoll;
  auto it = setcoll.cbegin();
</pre>

<p>But is it <em>better</em>?</p>

<p>To help answer that question let us consider the alternatives in more detail.</p>

<p>The original code is often seen as hard to read because the length of the variable declaration dwarfs the name itself. Many programmers dislike the way that the meaning of the code is masked by the scaffolding required to get the variable type correct.</p>

<p>Additionally, the code is fragile in the face of change. The type of the iterator is heavily dependent on the type of the underlying container so the two declarations (for <code>setcoll</code> and <code>it</code>) must remain in step if the type of one changes.</p>

<p>The second code, using a <code>typedef</code>, improves both the readability of the code and also the maintainability as, should the type of the container change, the nested type <code>const_iterator</code> governed by the <code>typedef</code> will change too. However, having to pick a type name adds to the cognitive overhead; additionally good names are notoriously hard to pin down.</p>

<p>In the final code the use of <code>auto</code> further helps readability by focussing the attention on the expression used to initialise <code>it</code> as this defines the type that <code>auto</code> will resolve to. Given this, code maintainability is improved as the type of <code>it</code> will track the type required by the initialising expression.</p>

<p>We retain the type safety of the language â€“ the variable is still strongly typed â€“ but implicitly not explicitly. The main downside of the final version of the code is that if you <em>do</em> need to know the precise type of the variable then you have to deduce it from the expression, to do which also means knowing the type of the container. On the other hand, it can be argued that to understand the semantics of the line of code you already have to know this information, so the new style has not in practice made understanding the code any more difficult.</p>

<p>In this case I am inclined to agree with this view and I can see little downside to the use of <code>
auto</code> to declare variables for iterators and other such entities. So:</p>

<ul>
<li>the code is quicker and easier to write and, arguably, to read</li>
<li>the purpose is not lost in the syntax</li>
<li>code generated is identical to the explicit type</li>
<li>the variable automatically changes type if the collection type changes</li>
</ul>

<p>However, the last point can be reworded as the variable <span style="text-decoration:line-through;">automatically</span> <strong>silently</strong> changes type if the collection type changes. In particular this can be an issue with the difference between a <code>const</code> and non-<code>const</code> container. Note that the C++11 code uses <code>cbegin()</code>:</p>

<pre class="programlisting">
  auto it = setcoll.cbegin();
</pre>

<p>If weâ€™d retained the used of <code>begin()</code> we would have got a <strong>modifiable</strong> iterator from a non-<code>const</code> collection. The C++03 code makes it explicit by using the actual type name:</p>

 <pre class="programlisting">
  std::vector&lt;std::set&lt;int&gt;&gt;::const_iterator it;</pre>

<p>The stress is slightly different and may mean making some small changes to some class interfaces, as with the addition of <code>cbegin()</code>.</p>


<h2>DRY example</h2>
<p><code>auto</code> allows you to specify the type name once. Consider this code:</p>

<pre class="programlisting">
    std::shared_ptr&lt;std::string&gt; str =
        std::make_shared&lt;std::string&gt;(&quot;Test&quot;);
</pre>

<ol>
<li>Weâ€™ve repeated <code>the std::string</code></li>
<li><code>make_shared</code> exists solely to create <code>std::shared_ptr</code> objects</li>
</ol>

<p>We can write it more simply as:</p>

<pre class="programlisting">
  auto str = std::make_shared&lt;std::string&gt;(&quot;Test&quot;);</pre>

<p>The resulting code is just over half as long to write (and read) and I donâ€™t think weâ€™ve lost any information. Additionally the code is easier to change.</p>

<p>Using <code>auto</code> rather than repeating the type is indicated most strongly when:</p>

<ul>
<li>the type names are long or complex</li>
<li>the types are identical or closely related</li>
</ul>

<p><code>auto</code> is less useful when:</p>

<ul>
<li>the type name is simple â€“ or important</li>
<li>the cognitive overhead on the reader of the code is higher</li>
</ul>

<p>So I think <code>auto</code> may be less useful in an example like that in Listing 1.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
// in some header
struct X {
 int *mem_var;
 void aMethod();
};

// in a cpp file
void X::aMethod() {
  auto val = *mem_var; // what type is val?
  ...
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>YMMV (Your mileage may vary) â€“ opinions differ here. The ease of answering the question about the type of <code>val</code> may also depend on whether you are using an IDE with type info.</p>

<p>For example, with Microsoft Visual Studio you get the type for the example in Listing 1 displayed in the mouse-over as shown in Figure 1.</p>

<table class="sidebartable">
	<tr>
		<td><img src="http://accu.org/content/images/journals/ol116/Orr/Orr-1.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 1</td>
	</tr>
</table>

<h2>Dependent return type example</h2>

<p><code>auto</code> can simplify member function definitions. Consider the class and member function definition in Listing 2.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
class Example
{
public:
  typedef int Result;

  Result getResult();
};

Example::Result Example::getResult()
{ return ...; }
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<p>We have to use the prefix of <code>Example::</code> for the return type <code>Result</code> as at this point in the definition the scope does not include <code>Example</code>. <code>auto</code> allows the removal of the class name from the return type.</p>

<p>The syntax is to place the <code>auto</code> where the return type would otherwise go, then follow the function prototype with <code>-&gt;</code> and the actual return type:</p>

<pre class="programlisting">
  auto Example::getResult() -&gt; Result
  { return ...; }664&quot;&gt;
</pre>

<p>Whether or not this makes the code clearer depends on factors including:</p>

<ul>
<li>familiarity</li>
<li>consistent use of this style.</li>
</ul>

<p>Personally, I still canâ€™t decide on this one. I think the new style is an improvement over the old one, but until use of C++11 is sufficiently widespread trying to use the style may simply result in a mix of the old and new styles being used. I do not think this would be a great step forward for existing code bases, but might be worth trying out for new ones.</p>

<h2>Polymorphism?</h2>

<p>One problem with <code>auto</code> is the temptation to code to the <em>implementation</em> rather than to the <em>interface</em>. If we imagine a class hierarchy with an abstract base class <code>Shape</code> and various concrete implementations such as <code>Circle</code> and <code>Ellipse</code>. We might write code like this:</p>

<pre class="programlisting">
  auto shape = make_shared&lt;Ellipse&gt;(2, 5);
  ...
  shape-&gt;minor_axis(3);</pre>

<p>The use of <code>auto</code> has made the generic variable <code>shape</code> to be of the explicit type shared pointer to <code>Ellipse</code>. This makes it too easy to call methods â€“ such as <code>minor_axis</code> above â€“ that are not part of the interface but of the implementation.</p>

<p>When the type of shape is â€˜shared pointer to the abstract base classâ€™, you canâ€™t make this mistake. (Aside: I think this is a bigger problem with <code>var</code> in C# than with <code>auto</code> in C++ but your experience may be different.) The trouble is that <code>auto</code> is too â€˜plasticâ€™ â€“ it fits the exact type that matches whereas <em>without</em> <code>auto</code> the author needs to make a decision about the most appropriate type to use. This doesnâ€™t only affect polymorphism: <code>const</code>, signed/unsigned integer types and sizes are other possible pinch points where the deduction of the type done by <code>auto</code> is not the best choice.</p>

<h2>What type is it?</h2>

<p>It is possible to go to the extreme of making everything in the program use <code>auto</code>, but Iâ€™m not convinced this is a good idea. For example, what does the program in Listing 3 do?</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
auto main() -&gt; int {
  auto i = '1';
  auto j = i * 'd';
  auto k = j * 100l;
  auto l = k * 100.;
  return l;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 3</td>
	</tr>
</table>

<p>It is all too easy to assume the <code>auto</code> types are all the same â€“ miss the promotion, the <code>'l'</code>
 or the <code>'.'</code>. Opinions also vary on whether writing <code>main</code> using <code>auto</code>
 aids readability â€“ I am not at all sure it does, especially given the large amount of existing code predating this use of <code>auto</code>.</p>
 
<p>You can use the <code>auto</code> rules (on some compilers) to tell you the type. For example, if we want to find out the actual type of <code>j</code> we could write this code:</p>

<pre class="programlisting">
  auto main() -&gt; int {
    auto i = '1';
    auto j = i * 'd', x = &quot;x&quot;;
    ...</pre>

<p>When compiled this will error as the type deduction for <code>auto</code> for the variables <code>j</code>
 and <code>x</code> produces inconsistent types. A possible error message is:</p>
 
<pre class="programlisting">
    error: inconsistent deduction for 'auto':
  'int' and then 'const char*'</pre>

<p>You may also be able to get the compiler to tell you the type by using template argument deduction, for example:</p>

<pre class="programlisting">
  template &lt;typename T&gt;
  void test() { T::dummy(); }
  
  auto val = '1';
  test&lt;decltype(val)&gt;();
</pre>

<p>This generates an error and the error text (depending on the compiler) is likely to include text such as:</p>

<p class="blockquote">see reference to function template instantiation 'void test&lt;char&gt;(void)' being compiled</p>

<h3>What are the actual rules?</h3>

<p>The meaning of an <code>auto</code> variable declaration follows the rules for template argument deduction.</p>

<p>We can consider the invented function template</p>

<pre class="programlisting">
  template &lt;typename T&gt;
  void f(T t) {}</pre>
  
<p>and then in the expression <code>auto val = '1';</code> the type of <code>val</code> is the same as that deduced for <code>T</code> in the call <code>f('1')</code>.</p>

<p>This meaning was picked for good reason â€“ type deduction can be rather hard to understand and it was felt that having a subtly different set of rules for <code>auto</code> from existing places where types are deduced would be a bad mistake. However, this does mean that the type deduced when using <code>auto</code> differs from a (naÃ¯ve) use of <code>decltype</code>:</p>

<pre class="programlisting">
  const int ci;
  auto val1 = ci;
  decltype(ci) val2 = ci;</pre>

<p><code>val1</code> is of type <code>int</code> as the rules for template argument deduction will drop the top-level <code>const</code>; but the type of <code>val2</code> will be <code>const int</code> as that is the declared type of <code>ci</code>.</p>

<h2>Adding modifiers to auto</h2>

<p>Variables declared using <code>auto</code> can be declared with various combinations of <code>const</code> and various orts of references. So whatâ€™s the difference?</p>

<pre class="programlisting">
  auto          i   = &lt;expr&gt;;
  auto const    ci  = &lt;expr&gt;;
  auto       &amp;  ri  = &lt;expr&gt;;
  auto const &amp;  cri = &lt;expr&gt;;
  auto       &amp;&amp; rri = &lt;expr&gt;;
</pre>

<p>As above, <code>auto</code> uses the same rules as template argument deduction so we can ask the equivalent question about what type is deduced for the following uses of a function template:</p>

<pre class="programlisting">
  template &lt;typename T&gt;;
    void f(T          i);
    void f(T const    ci);
    void f(T       &amp;  ri);
    void f(T const &amp;  cri);
    void f(T       &amp;&amp; rri);
</pre>

<p>The answer to the question is, of course, â€˜it dependsâ€™ ... especially for the <code>&amp;&amp;</code> case (which is an example of what Scott Meyers has named the â€˜Universal Referenceâ€™).</p>

<h3>const inference (values)</h3>

<p>Let us start by looking at a few examples of using <code>auto</code> together with <code>const</code> for simple value declarations.</p>

<pre class="programlisting">
  int i(0); int const ci(0);
  
  auto       v0 = 0;
  auto const v1 = 0;
  auto       v2 = i;
  auto const v3 = i;
  auto       v4 = ci;
  auto const v5 = ci;
</pre>

<p>This is the easiest case and, as in the earlier discussion of the difference between <code>auto</code> and <code>decltype</code>, <code>v0</code> is of type <code>int</code> and <code>v1</code> is of type <code>int const </code>(you may be more used to calling it <code>const int</code>). Similarly <code>v2</code> and <code>v4</code> are of type <code>int</code> and <code>v3</code> and <code>v5</code> are of type <code>int const</code>.</p>

<p>In general, with simple variable declarations, I prefer using <code>auto const</code> by default as the reader knows the value will remain fixed. This means if they see a use of the variable later in the block they do not have to scan the intervening code to check whether or not the value has been modified.</p>

<h3>const inference (references)</h3>

<p>Letâ€™s take the previous example but make each variable an l-value reference:</p>

<pre class="programlisting">
  int i(0); int const ci(0);
  
  auto       &amp; v0 = 0;  // Error
  auto const &amp; v1 = 0;
  auto       &amp; v2 = i;
  auto const &amp; v3 = i;
  auto       &amp; v4 = ci;
  auto const &amp; v5 = ci;
</pre>

<p>The first one <strong>fails</strong> as you may not form an l-value reference to a temporary value. However, you <strong>are</strong> allowed to form a <code>const</code> reference to a temporary and so <code>v1</code> is valid (and of type <code>int const &amp;</code>).</p>

<p><code>v2</code> is valid and is of type <code>int &amp;</code> and the three remaining variables are all of type <code>int const &amp;</code>. Notice that the <code>const</code> for <code>v4</code> is not removed, unlike in the previous example, as it is not a <em>top-level</em> use of <code>const</code>.</p>

<h3>Reference collapsing and auto</h3>

<p>Things get slightly more complicated again when we use the (new) r-value reference in conjunction with <code>
auto</code>.</p>

<pre class="programlisting">
  int i(0); int const ci(0);
  
  auto       &amp;&amp; v0 = 0;
  auto const &amp;&amp; v1 = 0;
  auto       &amp;&amp; v2 = i;
  auto const &amp;&amp; v3 = i;   // Error

  auto       &amp;&amp; v4 = ci;
  auto const &amp;&amp; v5 = ci;  // Error
</pre>

<p>The first variable, <code>v0</code>, becomes an r-value reference to the temporary 0 (type <code>int &amp;&amp;</code>) and the second, <code>v1</code>, is the <code>const</code> equivalent (<code>int const &amp;&amp;</code>). When it comes to <code>v2</code>, however, the reference type â€˜collapsesâ€™ to an l-value reference and so the type of <code>v2</code> is simply <code>int &amp;</code>. <code>v3</code> is invalid as the presence of the <code>const</code> suppresses the reference collapsing and you are not allowed to bind an r-value reference to an l-value. <code>v4</code> reference-collapses to <code>int const &amp;</code> and the declaration of <code>v5</code> is an error for the same reason as for <code>v3</code>.</p>

<p>So this is the complicated one: <code>auto &amp;&amp; var = &lt;expr&gt;;</code> as, depending on the expression, <code>var</code> could be</p>

<pre class="programlisting">
  T &amp;
  T &amp;&amp;
  T const &amp;
  T const &amp;&amp;
</pre>

<p>Deducing the last case is a little more obscure â€“ you need to bind to a <code>const</code> temporary that is of class type. Hereâ€™s an example of deducing <code>const &amp;&amp;</code>:</p>

<pre class="programlisting">
  class T{};
  const T x() { return T(); }
  auto &amp;&amp; var = x();   // var is of type T const &amp;&amp;
</pre>

<p>Note that non-class types, like <code>int</code>, decay to <code>&amp;&amp;</code>. This changed during the development of C++11 and at one point Microsoftâ€™s compiler and the Intellisense disagreed over the right answer (see Figure 2)!</p>

<table class="sidebartable">
	<tr>
		<td><img src="http://accu.org/content/images/journals/ol116/Orr/Orr-2.png" /></td>
	</tr>
	<tr>
		<td class="title">Figure 2</td>
	</tr>
</table>

<p>(The compiler in the Visual Studio 2013 preview edition does now get this right.)</p>

<h2>More dubious cases</h2>

<p><code>auto</code> does not work well with initializer lists as the somewhat complicated rules for parsing these results in behaviour, when used with <code>auto</code>, that may not be what you expect:</p>

<pre class="programlisting">
  int main() {
    int var1{1};
    auto var2{1};
</pre>

<p>You might expect <code>var1</code> and <code>var2</code> to have the same type. Sadly the C++ rules have introduced a new â€˜vexing parseâ€™ into the language. The type of <code>var2</code> is <code>std::initializer_list&lt;int&gt;</code>. There is a proposal to make this invalid as almost everyone who stumbles over this behaviour finds it unexpected.</p>

<p>A mix of signed and unsigned integers â€“ or integers of different sizes â€“ can cause problems with <code>auto</code>. In many cases the compiler generates a warning, if you set the appropriate flag(s), and if you heed the warning you can resolve possible problems. But not in all cases ....</p>

<pre class="programlisting">
  std::vector&lt;int&gt; v;
  ...
  for (int i = v.size() - 1; i &gt; 0; i -= 2)
  {
    process(v[i], v[i-1]);
  }
</pre>
  
<p>If you change <code>int</code> to <code>auto</code> then the code breaks. The trouble here is that <code>v.size()</code>
 returns <code>std::vector::size_type</code> which is an unsigned integer value. The rules for integer promotions means that <code>i</code> is also an unsigned integer value. If it starts out odd it will decrease by 2 round the loop as far as 1, then the next subtraction will wrap around â€“ to a large <strong>positive</strong> value. Of course, care must be taken to ensure that an <code>int</code> will be large enough for all possible values of <code>size()</code> that the program might encounter.</p>
 
<p>Iâ€™m less convinced by the use of <code>auto</code> for variables defined by the results of arithmetic expressions as the correct choice of variable type may be necessary to ensure the desired behaviour of the program.</p>

<h2>Conclusion</h2>

<p><code>auto</code> is a very useful tool in the programmerâ€™s armoury as it allows you to retain type safety without needing to write out the explicit types of the variables. I expect that use of <code>auto</code> will become fairly widespread once use of pre-C++11 compilers becomes less common.</p>

<p>However, I do have a concern that thoughtless use of <code>auto</code> may result in code that does not behave as expected, especially when the data type chosen implicitly is not the one the reader of the code anticipates.</p>
 
<p>Please donâ€™t use <code>auto</code> without thought simply to save typing, but make sure you use it by conscious choice and being aware of the potential issues and possible alternatives. </p>

<h2>Acknowledgements</h2>

<p>Many thanks to Rai Sarich and the <em>Overload</em> reviewers for their suggestions and corrections which have helped to improve this article.</p>

<p>This article is based on the presentation of that title at ACCU 2013.</p>

<p class="footnotes"></p>
<ol>
<li><a id="FN01"></a><code>cbegin</code> is another C++11 addition: it explicitly returns a <code>const</code> iterator even from a mutable container.</li>
</ol>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
