    <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  :: C++ Antipatterns</title>
        <link>https://members.accu.org/index.php/articles/2271</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 #134 - August 2016</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/c364/">o134</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+364/">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;C++ Antipatterns</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 03 August 2016 22:15:32 +01:00 or Wed, 03 August 2016 22:15:32 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Certain mistakes crop up frequently in C++. Jonathan Wakely offers some pro-tips to help you avoid common errors.</p>
<p><strong>Body:</strong>&nbsp;<p>This article documents some common mistakes that I see again and again in bug reports and requests for help on sites like StackOverflow.</p>

<h2>Reading from an istream without checking the result</h2>

<p>A very frequently asked question from someone learning C++ looks like this:</p>

<p class="blockquote">I wrote this program but it doesnâ€™t work. It reads from the file correctly but does the calculation wrong.</p>
<pre class="programlisting">
    #include &lt;iostream&gt;
    #include &lt;fstream&gt;
    
    int calculate(int a, int b, int c)
    {
      // blah blah blah complex calculation
      return a + b + c;
    }

    int main()
    {
      std::ifstream in(&quot;input.txt&quot;);
      if (!in.is_open())
      {
        std::cerr &lt;&lt; &quot;Failed to open file\n&quot;;
        return 1;
      }

      int i, j, k;
      in &gt;&gt; i &gt;&gt; j &gt;&gt; k;
      std::cout &lt;&lt; calculate(i, j, k);
    }</pre>
	
<p class="blockquote">Why doesnâ€™t the calculation work?</p>

<p>In many, many cases the problem is that the <code>in &gt;&gt; ...</code> statement failed, so the variables contain garbage values and so the inputs to the calculation are garbage.</p>

<p>The program has no way to check the assumption â€˜<em>it reads from the file correctly</em>â€™, so attempts to debug the problem are often just based on guesswork.</p>

<p>The solution is simple, but seems to be rarely taught to beginners: <strong>always check your I/O operations</strong>.</p>

<p>The improved version of the code in Listing 1 only calls <code>calculate(i, j, k)</code> if reading values into all three variables succeeds.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
int i, j, k;

if (in &gt;&gt; i &gt;&gt; j &gt;&gt; k)
{
  std::cout &lt;&lt; calculate(i, j, k);
}

else
{
  std::cerr &lt;&lt; 
    &quot;Failed to read values from the file!\n&quot;;
  throw std::runtime_error(&quot;Invalid input file&quot;);
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 1</td>
	</tr>
</table>

<p>Now if any of the input operations fails you donâ€™t get a garbage result, you get an error that makes the problem clear immediately. You can choose other forms of error handling rather than throwing an exception, the important bit is to check the I/O and not just keep going regardless when something fails.</p>

<p class="lesson"><strong>Recommendation</strong>: always check that reading from an istream succeeds.</p>

<h2>Locking and unlocking a std::mutex</h2>

<p>This is <strong>always</strong> wrong:</p>

<pre class="programlisting">
  std::mutex mtx;
  void func()
  {
    mtx.lock();
    // do things
    mtx.unlock();
  }</pre>
  
<p>It should <strong>always</strong> be done using one of the RAII scoped lock types such as <code>lock_guard</code> or <code>unique_lock</code> e.g.</p>

<pre class="programlisting">
  std::mutex mtx;
  void func()
  {
    std::lock_guard&lt;std::mutex&gt; lock(mtx);
    // do things
  }</pre>

<p>Using a scoped lock is exception-safe, you cannot forget to unlock the mutex if you return early, and it takes fewer lines of code.</p>

<p class="lesson"><strong>Recommendation</strong>: always use a scoped lock object to lock and unlock a mutex.</p>

<p>Be careful that you donâ€™t forget to give a scoped lock variable a name! This will compile, but doesnâ€™t do what you expect:</p>

<pre class="programlisting">
  std::mutex mtx;
  void func()
  {
    std::unique_lock&lt;std::mutex&gt; (mtx); // OOPS!
    // do things, but the mutex is not locked!
  }</pre>
  
<p>This default-constructs a <code>unique_lock</code> object called <code>mtx</code>, which has nothing to do with the global <code>mtx</code> object (the parentheses around <code>(mtx)</code> are redundant and so itâ€™s equivalent to simply <code>std::unique_lock&lt;std::mutex&gt; mtx;)</code>.</p>

<p>A similar mistake can happen using braces instead of parentheses:</p>

<pre class="programlisting">
  std::mutex mtx;
  void func()
  {
    std::unique_lock&lt;std::mutex&gt; {mtx};  // OOPS!
    // do things, but the mutex is not locked!
  }</pre>
  
<p>This <em>does</em> lock the global mutex <code>mtx</code>, but it does so in the constructor of a <em>temporary</em> <code>unique_lock</code>, which immediately goes away and unlocks the mutex again.</p>

<h2>Testing for istream.eof() in a loop</h2>

<p>A common mistake when using istreams is to try and use <code>eof()</code> to detect when there is no more input:</p>

<pre class="programlisting">
  while (!in.eof())
  {
    in &gt;&gt; x;
    process(x);
  }</pre>
  
<p>This doesnâ€™t work because the <code>eofbit</code> is only set <em>after</em> you try to read from a stream that has already reached EOF. When all the input has been read, the loop will run again, reading into <code>x</code> will fail, and then <code>process(x)</code> is called <em>even though nothing was read</em>.</p>

<p>The solution is to test whether the read succeeds, instead of testing for EOF:</p>

<pre class="programlisting">
  while (in &gt;&gt; x)
  {
    process(x);
  }</pre>
  
<p>You should never read from an istream without checking the result anyway, so doing that correctly avoids needing to test for EOF.</p>

<p class="lesson"><strong>Recommendation</strong>: test for successful reads instead of testing for EOF.</p>

<h2>Inserting into a container of smart pointers with emplace_back(new X)</h2>

<p>When appending to a <code>std::vector&lt;std::unique_ptr&lt;X&gt;&gt;</code>, you cannot just say <code>v.push_back(new X)</code>, because there is no implicit conversion from <code>X*</code> to <code>std::unique_ptr&lt;X&gt;</code>.</p>

<p>A popular solution is to use <code>v.emplace_back(new X)</code> because that compiles (<code>emplace_back</code> constructs an element in-place from the arguments, and so can use <code>explicit</code> constructors).</p>

<p>However, this is not safe. If the vector is full and needs to reallocate memory, that could fail and throw a <code>bad_alloc</code> exception, in which case the pointer will be lost and will never be deleted.</p>

<p>The safe solution is to create a temporary <code>unique_ptr</code> that takes ownership of the pointer before the vector might try to reallocate:</p>

<pre class="programlisting">
  v.push_back(std::unique_ptr&lt;X&gt;(new X))</pre>
  
<p>(You could replace <code>push_back</code> with <code>emplace_back</code> but there is no advantage here because the only conversion is explicit anyway, and <code>emplace_back</code> is more typing!)</p>

<p>In C++14 you should just use <code>std::make_unique</code> and itâ€™s a non-issue:</p>

<pre class="programlisting">
  v.push_back(std::make_unique&lt;X&gt;())</pre>
    
<p class="lesson"><strong>Recommendation</strong>: do not prefer <code>emplace_back</code> just because it allows you to call an <code>explicit</code> constructor. There might be a good reason the class designer made the constructor <code>explicit</code> that you should think about and not just take a short cut around it.</p>

<p>(Scott Meyers discusses this point as part of Item 42 in <em>Effective Modern C++</em>.)</p>


<h2>Defining â€˜less thanâ€™ and other orderings correctly</h2>

<p>When using custom keys in maps and sets, a common mistake is to define a â€˜less thanâ€™ operation as in Listing 2.</p>

<table class="sidebartable">
	<tr>
		<td>
			<pre class="programlisting">
struct X
{
  int a;
  int b;
};

inline bool operator&lt;(const X&amp; l, const X&amp; r)
{
  if (l.a &lt; r.a)
    return true;
  if (l.b &lt; r.b)
    return true;
  return false;
}
			</pre>
		</td>
	</tr>
	<tr>
		<td class="title">Listing 2</td>
	</tr>
</table>

<p>This <code>operator&lt;</code> does not define a valid ordering. Consider how it behaves for <code>X{1, 2}</code> and <code>X{2, 1}</code>:</p>

<pre class="programlisting">
  X x1{1, 2};
  X x2{2, 1};
  assert( x1 &lt; x2 );
  assert( x2 &lt; x1 );</pre>
  
<p>The <code>operator&lt;</code> defined above means that <code>x1</code> is less than <code>x2</code> but also that <code>x2</code> is less than <code>x1</code>, which should be impossible!</p>

<p>The problem is that the <code>operator&lt; </code>says that <code>l</code> is less than <code>r</code> if <em>any</em> member of <code>l</code> is less than the corresponding member of <code>r</code>. Thatâ€™s like saying that 20 is less than 11 because when you compare the second digits '0' is less than '1' (or similarly, that the string &quot;20&quot; should be sorted before &quot;11&quot; because the second character '0' is less than the second character '1').</p>

<p>In technical terms the <code>operator&lt;</code> above fails to define a <em>Strict Weak Ordering</em>.</p>

<p>Another way to define a bogus order is:</p>

<pre class="programlisting">
  inline bool operator&lt;(const X&amp; l, const X&amp; r)
  {
    return l.a &lt; r.a &amp;&amp; l.b &lt; r.b;
  }</pre>
  
<p>Where the first example gave the nonsensical result:</p>

<pre class="programlisting">
  x1 &lt; x2 &amp;&amp; x2 &lt; 1</pre>
  
<p>this definition gives the result:</p>

<pre class="programlisting">
  !(x1 &lt; x2) &amp;&amp; !(x1 &lt; x2)</pre>

<p>In other words, the two values are considered to be equivalent, and so only one of them could be inserted into a unique associative container such as a <code>std::set</code>. But then if you compare those values to <code>X x3{1, 0}</code>, you find that <code>x1</code> and <code>x3</code> are equivalent, but <code>x1</code> and <code>x2</code> are not. So depending which of <code>x1</code> and <code>x2</code> is in the <code>std::set </code>affects whether or not you can add <code>x3</code>!</p>

<p>An invalid order like the ones above will cause undefined behaviour if it is used where the Standard Library expects a correct order, e.g. in <code>std::sort</code>, <code>std::set</code>, or <code>std::map</code>. Trying to insert the <code>x1</code> and <code>x2</code> values above into a <code>std::set&lt;X&gt;</code> will give strange results, maybe even crashing the program, because the invariant that a setâ€™s elements are always sorted correctly is broken if the comparison function is incapable of sorting correctly.</p>

<p>This is discussed further in <em>Effective STL</em> by Scott Meyers, and in the <em>CERT C++ Coding Standard.</em></p>

<p>A correct implementation of the function would only consider the second member when the first members are equal i.e.</p>

<pre class="programlisting">
  inline bool operator&lt;(const X&amp; l, const X&amp; r)
  {
    if (l.a &lt; r.a)
      return true;
    if (l.a == r.a &amp;&amp; l.b &lt; r.b)
      return true;
    return false;
  }</pre>
  
<p>Since C++11 defining an order correctly is trivial, just use <code>std::tie</code>:</p>

<pre class="programlisting">
  inline bool operator&lt;(const X&amp; l, const X&amp; r)
  {
    return std::tie(l.a, l.b) &lt; std::tie(r.a, r.b);
  }</pre>

<p>This creates a tuple referring to the members of <code>l</code> and a tuple referring to the members of <code>r</code>, then compares the tuples, which does the right thing (only comparing later elements when the earlier ones are equal).</p>

<p class="lesson"><strong>Recommendation</strong>: When writing your own less-than operator make sure it defines a Strict Weak Ordering, and write tests to ensure that it doesnâ€™t give impossible results like <code>(a &lt; b &amp;&amp; b &lt; a)</code> or <code>(a &lt; a)</code>. Prefer using <code>std::tie()</code> to create tuples which can be compared, instead of writing your own error-prone comparison logic.</p>

<p>Consider whether defining a hash function and using either</p>

<ul>
	<li><code>std::unordered_set</code></li>
	
	<li><code>std::unordered_map</code></li>
</ul>

<p>would be better anyway than </p>

<ul>
	<li><code>std::set</code></li>
	
	<li><code>std::map</code> </li>
</ul>

<p>Unordered containers require an equality operator, but thatâ€™s harder to get wrong.</p>

<h2>In summary</h2>

<p>The only common theme to the items above is that I see these same mistakes made again and again. I hope documenting them here will help you to avoid them in your own code, and in code you review or comment on. If you know of other antipatterns that could be covered let me or the <em>Overload</em> team know about them, so they can be included in a follow-up piece (or write a follow up piece yourself !).</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
