    <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  :: A Return Type That Doesnâ€™t Like Being Ignored</title>
        <link>https://members.accu.org/index.php/journals/2012</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 #53 - Feb 2003 + Programming Topics</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/c193/">53</a>
                    (9)
<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/c193-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c193+65/">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;A Return Type That Doesnâ€™t Like Being Ignored</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 04 February 2003 22:59:25 +00:00 or Tue, 04 February 2003 22:59:25 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<h2>list&lt;type&gt;::insert</h2>
<p>A while ago I finished a contract to write a small subset of STL targeted at embedded C++. This proved an interesting challenge. For example, in full STL, you insert a value into a list using, youâ€™ll never guess, <code>list::insert</code>.</p>

<pre><code>template&lt;typename type&gt;
list&lt;type&gt;::iterator
list&lt;type&gt;::insert(iterator position, const type&amp; value) {
...
}
</code></pre>

<p>This returns an iterator to the inserted copy of the value or it throws an exception (allocating the internal list node could throw <code>std::bad_alloc</code> for example). However, you canâ€™t use this <code>insert</code> in embedded C++ for two reasons.</p>
<ol>
<li>
<p>Embedded C++ does not support templates. I regard this as a minor problem. In this case templates are being used to get the compiler to write code. You can fake this use of templates quite easily if you put your mind to it.</p>
</li>
<li>
<p>Embedded C++ does not support exceptions. This is definitely not a minor problem. What to do?</p>
</li>
</ol>
<h2>The role of failure</h2>
<p>My first choice was to emulate exceptions. However, for various understandable reasons, my client decided not to adopt an exception-like mechanism. Iâ€™ll explain what I did shortly, but first Iâ€™d like to make a short diversion and explain why emulating exceptions is my strong preference. Itâ€™s all to do with failure [1].</p>
<p>The role of failure is fundamental to writing good software. When you use STL <code>list&lt;type&gt;::insert</code> you donâ€™t have to worry about its return value if it fails because if it fails it doesnâ€™t return. This allows you to write sequences of expressions and statements without having to constantly check if anything went wrong. This is really important.</p>

<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">#ifndef LOUD_BOOL_INCLUDED
#define LOUD_BOOL_INCLUDED

class loud_bool {
    public: // construction/destruction

    loud_bool(bool decorated);
    loud_bool(const loud_bool&amp;);
    ~loud_bool();

public:     // conversion
    operator bool() const;
private:    // inappropriate
    loud_bool&amp; operator=(const loud_bool&amp;);
private:    // state
    bool result;
    mutable bool ignored;
};
#endif
</pre>
</td>
</tr>
<tr>
<td class="title">Listing 1: loud_bool.hpp</td>
</tr>
</table>

<p>To use an analogy, consider taking a short walk to the local shop for a pint of milk. Do you:</p>
<p>a) check whether you havenâ€™t died after every step, or<br>
b) just walk to the shop and hope you donâ€™t die.</p>
<p>Answer; you just walk to the shop of course. No checks if youâ€™ve died yet. If something serious happens (eg youâ€™re hit by a bus) then you wonâ€™t get to the shop but frankly you wonâ€™t care much about the milk by then anyway. If youâ€™re still alive youâ€™ll be much more concerned about getting to the nearest hospital.</p>
<p>The point is that constantly checking if youâ€™ve died is silly not because it slows your journey so massively but because itâ€™s exactly the wrong thing to be concerned about in the first place.</p>
<p>The right thing to be concerned about is what happens if you are hit by a bus. The reality is simple. If you <strong>are</strong> hit by a bus you wonâ€™t be in much of a state to do anything so the only sensible and practical approach is to make arrangements <strong>before</strong> you set off.</p>
<p>My conclusion is this: The software model of use that exceptions bring with them is so valuable that if youâ€™re working in an environment (or language subset) that doesnâ€™t support exceptions you should consider ways in which you can emulate them. The alternative is constantly checking if youâ€™ve died.</p>
<h2>Back to the story</h2>
<p>But as I said, my client decided not to adopt an exception-like mechanism. So what did I do? I considered making <code>list::insert</code> return an iterator equal to <code>end()</code> if the insertion failed but decided this was not a good idea. When things are different they should be clearly different.</p>

<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">#include &quot;loud_bool.hpp&quot;
#include &lt;ios&gt;
#include &lt;iostream&gt;

loud_bool::loud_bool(bool decorated)
    : result(decorated),
      ignored(true) {
    // all done
}

loud_bool::loud_bool(const loud_bool &amp; other)
    : result(other.result),
      ignored(other.ignored) {
    other.ignored = false;
}

loud_bool::~loud_bool() {
    if (ignored) {
        std::cerr.setf(std::ios_base::boolalpha);
        std::cerr &lt;&lt; &quot;WARNING: return &quot;
                  &lt;&lt; result
                  &lt;&lt; &quot;; is being ignored&quot;
                  &lt;&lt; std::endl;
    }
}

loud_bool::operator bool() const {
    ignored = false;
    return result;
}
</pre>
</td>
</tr>
<tr>
<td class="title">Listing 2: loud_bool.cpp</td>
</tr>
</table>

<p>One option is to return a <code>bool</code> and a <code>list::iterator</code> inside a <code>pair</code>-like structure. However, as it turned out, my clientâ€™s particular list requirement didnâ€™t require the returned iterator, so the version of <code>list&lt;type&gt;::insert</code> I wrote looked like this:</p>
<pre><code>bool list::insert(iterator position, const type&amp; value);
</code></pre>

<p>Lacking exceptions, this <code>list::insert</code> returns a <code>bool</code> to signify success or failure. The problem now of course is that the <code>bool</code> return is just too easy to ignore. In contrast, exceptions, by design, are impossible to ignore. In full STL if youâ€™re not interested in the iterator return value, ignoring it is precisely what you do. I thought about this for a while before realizing there was a solution. The problem is that if you ignore a <code>bool</code> nothing happens. So the answer is not to use a <code>bool</code>! Use something that looks like a <code>bool</code>, smells like a <code>bool</code>, feels like a <code>bool</code>, and shouts loudly when ignored. (In practice, this version for embedded C++ has to be modified slightly because embedded C++ does not support the <code>mutable</code> keyword either.) See Listings 1-3.</p>
<p>The <code>loud_bool</code> class generalizes to the template class shown in Listings 4-6 for those of you working in full C++ (Iâ€™ve not put it in a namespace to save horizontal space).</p>

<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">#include &quot;loud_bool.hpp&quot;

loud_bool example1() {
    //...
    return true;
}

int main() {
    bool result = example1();   //no warning
    example1();                 //generates warning return 0;
}
</pre>
</td>
</tr>
<tr>
<td class="title">Listing 3: Example use of loud_bool</td>
</tr>
</table>

<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">#ifndef WARN_IF_IGNORED_INCLUDED
#define WARN_IF_IGNORED_INCLUDED

template&lt;typename result_type&gt;
class warn_if_ignored
{
public:     // construction/destruction
    warn_if_ignored(const result_type&amp; decorated);
    warn_if_ignored(const warn_if_ignored&amp;);
    ~warn_if_ignored();
public:     // conversion
    operator result_type() const;
private:    // inappropriate
    warn_if_ignored&amp; operator=(const warn_if_ignored&amp;);
private:    // state
    result_type result;
    mutable bool ignored;
};

#include &quot;warn_if_ignored-template.hpp&quot;
#endif
</pre>
</td>
</tr>
<tr>
<td class="title">Listing 4: warn_if_ignored.hpp</td>
</tr>
</table>

<p>The final step (left as exercise for the reader) is to parameterize the behaviour if the result is ignored.</p>

<p>Jon Jagger<br>
jon@jaggersoft.com</p>

<p>References
[1] <em>To Engineer is Human. The Role of Failure in Successful Design.</em> Henry Petroski. Vintage. 0-679-73416-3</p>


<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">#include &lt;ios&gt;
#include &lt;iostream&gt;

template&lt;typename result_type&gt;
warn_if_ignored&lt;result_type&gt;::warn_if_ignored( const result_type &amp; decorated)
    : result(decorated),
      ignored(true) {
    // all done
}

template&lt;typename result_type&gt;
warn_if_ignored&lt;result_type&gt;::warn_if_ignored( const warn_if_ignored &amp; other)
    : result(other.result),
      ignored(other.ignored) {
    other.ignored = false;
}

template&lt;typename result_type&gt;
warn_if_ignored&lt;result_type&gt;::~warn_if_ignored() {
    if (ignored) {
        std::cerr &lt;&lt; &quot;WARNING: return &quot;
                  &lt;&lt; result
                  &lt;&lt; &quot;; is being ignored&quot;
                  &lt;&lt; std::endl;
    }
}

template&lt;typename result_type&gt;
warn_if_ignored&lt;result_type&gt;::operator result_type() const {
    ignored = false;
    return result;
}
</pre>
</td>
</tr>
<tr>
<td class="title">Listing 5: warn_if_ignored-template.hpp</td>
</tr>
</table>

<table class="sidebartable">
<tr>
<td>
<pre class="programlisting">#include &quot;warn_if_ignored.hpp&quot;

warn_if_ignored&lt;bool&gt; example2() {
    // ...
    return false;
}

int main() {
    bool result = example2();   //no warning
    example2();                 //generates warning return 0;
}
</pre>
</td>
</tr>
<tr>
<td class="title">Listing 6: Example use of warn_if_ignored template</td>
</tr>
</table>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
