    <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  :: Compile Time Indirection - An Unusual Template
Technique</title>
        <link>https://members.accu.org/index.php/articles/452</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 #42 - Apr 2001</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/c162/">42</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+162/">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;Compile Time Indirection - An Unusual Template
Technique</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 April 2001 17:46:05 +01:00 or Thu, 26 April 2001 17:46:05 +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>It is a common idiom in C++ to write a class whose sole data
member is a pointer to another class that provides the
implementation. Originally called &quot;The Cheshire Cat Idiom&quot; this has
more recently been dubbed &quot;Compilation Firewall&quot; and &quot;The Pimpl
Idiom&quot;. This last comes from the following paradigm:</p>
<pre class="programlisting">
// Header file
class interface {
  /* omitted */
private:
  class impl;
  impl* pimpl;  
} ;
// Implementation file
class interface::impl {
  /* omitted */
} ;
</pre>
<p>However, this is all by way on a preamble - since the situation
I want to address is motivated less by implementation hiding and
more by the desire to provide a variable implementation. For
example. we may want to vary between implementations <tt class=
"classname">state1</tt>, <tt class="classname">state2</tt> and
<tt class="classname">state3</tt> at run time. (As the names
suggest this is one reasonable way to implement state
machines.)</p>
<p>The only difficulty with this is that we can't write:</p>
<pre class="programlisting">
class state1 : public interface::impl {
...
</pre>
<p>because <tt class="literal">interface::impl</tt> is
inaccessible.</p>
<p>There are two obvious approaches to resolving this
inaccessibility issue. Both of these are inelegant because
implementation details become exposed in the header file.</p>
<p>The first solution is to make <tt class="literal">impl</tt> a
public member of <tt class="literal">interface</tt>. Making
<tt class="literal">impl</tt> public pollutes the client
interface:</p>
<pre class="programlisting">
// Header file
class interface {
  /* omitted */
  class impl;
private:
  impl* pimpl;  
} ;
// Implementation file
class interface::impl {
  /* omitted */
} ;
// . . .
class state1 : public interface::impl {
  /* omitted */
} ;
</pre>
<p>The second approach is to forward declare <tt class=
"classname">state1</tt> (and kin) and make them friends. This
exposes even more of the implementation details in the header
file:</p>
<pre class="programlisting">
// Header file
class state1;
class interface {
  /* omitted */
private:
  friend state1;
  class impl;
  impl* pimpl;  
} ;
// Implementation file
class interface::impl {
  /* omitted */
} ;
// . . .
class state1 : public interface::impl {
  /* omitted */
} ;
</pre>
<p>Neither approach is a disaster, but the inelegance niggled at me
until I realised there was a third solution that retains the
original form of the header file:</p>
<pre class="programlisting">
// Header file
class interface {
  /* omitted */
private:
  class impl;
  impl* pimpl;  
} ;
// Implementation file
class interface::impl {
  /* omitted */
} ;
// . . .
template&lt;class impl&gt;
class state1 : public impl {
/* omitted */
} ;
// . . .
interface::interface(): pimpl(new state1&lt;impl&gt;)
// . . .
</pre>
<p>This works because <tt class="literal">interface</tt> has access
to <tt class="literal">impl</tt> and can use it as a template
parameter and because <tt class="classname">state1&lt;&gt;</tt> has
access to its template parameter.</p>
<p>In C++ the template mechanisms are most commonly used to support
genericity. The technique illustrated here is unusual in that it
uses them instead to promote encapsulation.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
