    <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  :: Addendum to &quot;Tiny Template Tidbit&quot;</title>
        <link>https://members.accu.org/index.php/articles/358</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 #54 - Apr 2003</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/c157/">54</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+157/">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;Addendum to &quot;Tiny Template Tidbit&quot;</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 April 2003 22:57:18 +01:00 or Wed, 02 April 2003 22:57:18 +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="d0e18" id="d0e18"></a></h2>
</div>
<p>I would like to add a few comments to the Overload 47 article
that I wrote, entitled &quot;Tiny Template Tidbit&quot;. They don't change
conclusions of the article but may prevent some confusion.</p>
<p>First, as kindly pointed out to me by Alan Griffiths and Chris
Main, I misuse the term &quot;partial specialization&quot;.</p>
<p>On page 16, 2nd column, I say &quot;what we need is a template
overload for pointers&quot;. No problem so far. I then explain briefly
that it is somewhat like partial specialization available for
classes but not the same. Unfortunately, the code examples contain
C++ comments that describe some syntax as &quot;partial specialization&quot;
applied to functions (not supported in C++). They should instead
read &quot;overload for ...&quot;. I must have had a few beers too many that
day.</p>
<p>Secondly, a minor mistake in the formatting of the two lines
&quot;Advantages:&quot; and &quot;Disadvantages:&quot;. The typeface makes it look like
there are three paragraphs of advantages and almost two pages of
disadvantages! The disadvantages use up only three paragraphs as
well.</p>
<p>Finally, an interesting mistake was pointed out to me by Chris
Main. It appears that a bug in the SGI MipsPro C++ compiler allows
the compiler to pick the intended <tt class=
"function">getCoord()</tt> function template even though, according
to the Standard, it should not be able to. This is worth expanding
upon. Let me recall the summary here:</p>
<p>I showed how a processor (object or function) that uses data
from a set of objects of a certain type can be generalized with a
very small number of lines of code, using templates and
specializations, such that the processor doesn't need to be changed
when the data structure changes. More specifically, your processor
object or function is able to extract the needed data from the
container elements, regardless of whether the container element
type</p>
<div class="orderedlist">
<ol type="1">
<li>
<p><span class="emphasis"><em>Is</em></span> the data, or is a
pointer to the data (as opposed to containing it)</p>
</li>
<li>
<p>Is an object or a pointer to an object, <span class=
"emphasis"><em>containing</em></span> the data</p>
</li>
<li>
<p>Makes data available as <span class=
"emphasis"><em>attribute</em></span> OR <span class=
"emphasis"><em>method</em></span> (which we don't address in this
addendum)</p>
</li>
</ol>
</div>
<p>Finally, the compiler does this for you automatically, without
requiring your intervention.</p>
<p>Two of the functions and templates used were:</p>
<pre class="programlisting">
/// General function template
template &lt;class P&gt; inline
const Coord&amp; getCoord(const P&amp; p) {
  return p.coords;
}

/// Overload for pointers to things
template &lt;class P&gt;
const Coord&amp; getCoord(const P* p) {
  return p-&gt;coords;
}
</pre>
<p>where P is one of the six types of data described in 1-3 above.
As Chris pointed out to me, given the code fragment</p>
<pre class="programlisting">
Struct Point {Coord coord;};
Point coord;
getCoord(&amp;coord);
</pre>
<p>the compiler has two choices for <tt class=
"function">getCoord()</tt>:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p><tt class="literal">getCoord&lt;Point*&gt;(const
Point*&amp;)</tt></p>
</li>
<li>
<p><tt class="literal">getCoord&lt;Point&gt;(const Point*)</tt></p>
</li>
</ol>
</div>
<p>Which one will the compiler choose? The one we want (#2) or #1?
With SGI's mipspro compiler, it chooses #2. With gcc 2.95, it
chooses #1. I thought it was a bug with gcc but Chris gives good
evidence that it is the other way around: overload resolution
states that the compiler must choose the most &quot;specific&quot; template,
which is #1, since the parameter is a pointer to the type rather
than just the type.</p>
<p>This could seem like a major problem for the techniques
discussed in that article but really it isn't because the concepts
used were sound, it's just the technique used to implement them
that got side-tracked onto a wrong path. The fundamental problem is
how to tell the compiler to deal with two separate cases of data,
one a type, the other a pointer to a type (a &quot;pointer type&quot;). This
is easy with partial specialization of a class that does nothing
for types, and does a dereference for a pointer type:</p>
<pre class="programlisting">
template &lt;typename P&gt;
struct Obj {
  static const P&amp; getObj(const P&amp; p) {
    return p;
  }
};
// partial specialization for pointer
// types
template &lt;typename P&gt;
struct Obj&lt;P*&gt; {
  static const P&amp; getObj(const P* p) {
    return *p;
  }
};
</pre>
<p>They replace two of the four <tt class=
"function">getCoord()</tt> overloads mentioned in the article,
namely the ones with pointer types as parameter, and are used by
calling</p>
<pre class="programlisting">
getCoord( Obj&lt;T&gt;::getObj(p) )
</pre>
<p>instead of</p>
<pre class="programlisting">
getCoord(p)
</pre>
<p>In any case, I hope you found the article interesting and maybe
even useful. Thanks to Alan and Chris for sending feedback. As
usual, I find that writing articles is fun and challenging, but I
inevitably seem to learn a lot more than I could have expected from
the feedback of readers. I encourage you to give it a try, write an
article for Overload!</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e99" id="d0e99"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Stroustrup" id="Stroustrup"></a>
<p class="bibliomixed">[Stroustrup] Bjarne Stroustrup, <span class=
"citetitle"><i class="citetitle">C++ Programming
Language</i></span>, 3rd ed.</p>
</div>
<div class="bibliomixed"><a name="Alexandrescu" id=
"Alexandrescu"></a>
<p class="bibliomixed">[Alexandrescu] Andrei Alexandrescu,
<span class="citetitle"><i class="citetitle">Modern C++
Design</i></span></p>
</div>
<div class="bibliomixed"><a name="Schoenborn" id="Schoenborn"></a>
<p class="bibliomixed">[Schoenborn] Oliver Schoenborn, &quot;Tiny
Template Tidbit,&quot; <span class="citetitle"><i class=
"citetitle">Overload 47</i></span></p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
