    <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  :: From Mechanism to Method - Function Follows Form</title>
        <link>https://members.accu.org/index.php/journals/407</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 #48 - Apr 2002 + 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/c197/">48</a>
                    (7)
<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/c197-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c197+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;From Mechanism to Method - Function Follows Form</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 April 2002 17:46:09 +01:00 or Fri, 26 April 2002 17:46:09 +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>Is programming the manufacture of code? I would suggest that of
all the metaphors applied to the development of software,
manufacturing rates as perhaps one of the least useful and most
harmful. Where in the manufacturing metaphor is the idea that
programming is an act of communication? And not just with the
compiler. Code is more often read than written, and writing code is
just that: writing. You are an author with an audience: Today it
may be just you and the compiler, but tomorrow it will include
others&hellip; Which includes you: the &quot;What was I thinking when I
wrote this?&quot; or &quot;Which idiot wrote this? &hellip; Oh&quot; syndrome.</p>
<p>This perspective lends a quite different weight to the use of
language features in a program. In C++ we have a formal notation
for working with concepts as close to or as far from the metal as
we chose. The compiler cares little for how clearly we write the
code, how fit for purpose it is, or how we work in teams to develop
systems. It cares only for the well-formedness of the code as C++
(or at least the compiler's closest approximation). All those
other, non-functional considerations are about code as a means of
communication with others.</p>
<p>C++ offers an extensive shopping list of mechanisms. It is left
to the programmer to make sense - and sensible use - of these,
bringing method and clarity to bear on the expression of code,
coding to communicate intent idiomatically to others. But too often
we find that code looks like, well, code: a cipher whose key is
known privately and exclusively to its author - and sometimes,
alas, even this much is not true.</p>
<p>Overloading - especially operator overloading - is one of those
mechanisms that, when first encountered, can raise eyebrows and
open mouths. This response comes in two opposing flavours: &quot;Great!
I can see that we could use this all over the system&quot; or &quot;Oh no, I
don't think that's for us. Sounds different to what we normally
do&hellip; too radical&quot;. The former can often lead to the most
cunning of ciphers with the clarity of hieroglyphics (pre Rosetta
Stone); the latter to verbose code that misses the effectiveness of
established idioms and the benefits of template-based generic
programming. There is, however, a centre ground of practice between
these two.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>Principles</h2>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p>Express coordinate ideas in similar form.</p>
<p>This principle, that of parallel construction, requires that
expressions similar in context and function be outwardly similar.
The likeness of form enables the reader to recognize more readily
the likeness of content and function. Strunk and White [<a href=
"#Strunk-1979">Strunk-1979</a>]</p>
</blockquote>
</div>
<p>This advice works as well with the written keyword as it does
with the written word. It expresses the idea that similar
constructs should have similar meanings, a goodness of fit between
intent and realization, interface and implementation, reader and
writer. This principle of substitutability [<a href=
"#Liskov1987">Liskov1987</a>] is often expressed with respect to
inheritance and runtime polymorphism [<a href=
"#Coplien1992">Coplien1992</a>], but applies equally well to the
compile-time polymorphism you have with conversions, overloads, and
templates [<a href="#Henney2000a">Henney2000a</a>].</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e50" id="d0e50"></a>Common Name
Implies Common Purpose</h2>
</div>
<p>The principle that overloaded functions work to similar ends is
the one that makes the most sense of this feature. As a practice it
frees programmers from mangling names to distinguish otherwise
similar functions with differing argument lists (this is the job of
the compiler).</p>
<p>Following well-established conventions, where possible, clearly
makes sense. For instance, the standard C++ library establishes a
common set of names and semantics, conventions clarifying that
<tt class="methodname">empty</tt> means &quot;is empty?&quot; not &quot;to empty&quot;,
<tt class="methodname">clear</tt> means &quot;to clear&quot; and not &quot;is
clear?&quot;, etc. Note that judgement and resourcefulness are still
needed:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The standard defines a relatively small set of names, clearly
not enough to cover your whole domain of application.</p>
</li>
<li>
<p>The standard is not always consistent in its use of names, e.g.
the unhelpfully named <tt class="methodname">get</tt> member
function in <tt class="classname">auto_ptr</tt> is a query without
a side effect, whereas <tt class="methodname">get</tt> on a
<tt class="classname">basic_istream</tt> is a query with a
significant side effect.</p>
</li>
<li>
<p>There are other well-established sources of terminology that
provide names you can draw upon. There may be times these clash
with the standard. For example, depending on context, begin yields
an iterator or initiates a transaction. It is for you to determine
whether or not such overloading of meaning, as well as name, is
clear.</p>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e85" id="d0e85"></a>Operator
Underloading</h2>
</div>
<p>Whatever care is applied to the use of named function
overloading applies doubly so to operator overloading. It can be a
fertile ground for fertile imaginations. An opportunity to
communicate clearly or to resurrect a Tower of Babel.</p>
<p>The built-in types both set expectations in the reader and offer
a spec for the writer: &quot;When in doubt, do as the <tt class=
"type">int</tt>s do&quot; [<a href="#Meyers1996">Meyers1996</a>]. As
with any style principle, this one is elasticated: <tt class=
"methodname">operator+</tt> for the standard <tt class=
"classname">basic_string</tt> is not commutative, but its meaning
is clear nonetheless. Bitshift operators, <tt class=
"methodname">operator&lt;&lt;</tt> and <tt class=
"methodname">operator&gt;&gt;</tt>, for I/O stream insertion and
extraction stretches the elastic taut by an appeal to scripting
notations. However, the long history and established presence in
the standard library qualifies this idiom as effectively built-in.
Do not assume the same distinguished fate awaits any other
'creative' operator deployment! So, as a corollary, it may be worth
considering that when in serious doubt, do not do it.</p>
<p>In deciding the suitability or otherwise of operator
overloading, keep in mind that it only really makes sense for
value-based [<a href="#Henney2000b">Henney2000b</a>] rather than
indirection-based objects. Value-based objects represent
fine-grained information concepts, typically live on the stack or
embedded within other objects, and are passed around by copy or
const reference. Syntactically this emphasizes their value and
allows easy use of operators. Indirection-based objects, by
contrast, represent more significant chunks of system information
or behaviour, typically live on the heap, and are passed around by
pointer. Syntactically this emphasizes their identity but makes use
of operators awkward: having to dereference the pointer explicitly
before being able to use an operator somewhat defeats the intended
transparency of operator overloading.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e115" id="d0e115"></a>Smart
Pointers</h2>
</div>
<p>One of the most common C++ idioms involving overloading is the
SMART POINTER, ranging from reference-counted pointers to the
essentially simple but surprisingly intricate standard <tt class=
"classname">auto_ptr</tt>. However, it is a common myth that all
smart pointers are concerned with memory management, and that all
smart pointers support <tt class="methodname">operator-&gt;</tt>
and <tt class="methodname">operator*</tt> as their pointer-like
operations.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e129" id="d0e129"></a>The Three
Rs</h2>
</div>
<p>Use determines definition, and clearly not all smart pointers
are intended for the same use. We can consider operators for
pointers in three categories, the three Rs: (de)referencing,
relational, and arithmetic. According to purpose, we can select if
and how we provide these:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Dereferencing comes in the familiar forms of <tt class=
"methodname">operator*</tt> and <tt class=
"methodname">operator-&gt;</tt>, as well as the less familiar and
often overlooked <tt class="methodname">operator-&gt;*</tt>
[<a href="#Meyers1999">Meyers1999</a>] and <tt class=
"methodname">operator()</tt>.</p>
</li>
<li>
<p>Relational operators make sense for pointer or smart pointers
that have a natural ordering, such as raw pointers in the same
array or random access iterators. Having only equality (and hence
inequality) comparison makes sense for many other pointers, such as
reference-counted pointers. They typically test for identity rather
than value, which is why <tt class="classname">auto_ptr</tt> does
not support such comparison: Exclusive ownership means that in a
well-formed system <tt class="classname">auto_ptr</tt> equality
comparison will always return <tt class="literal">false</tt>.</p>
</li>
<li>
<p>Pointer arithmetic, such as <tt class=
"methodname">operator++</tt> and <tt class=
"methodname">operator+</tt>, makes sense for smart pointers that
encapsulate some concept of interval or progression, such as
iterators.</p>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e174" id="d0e174"></a>Function
Objects</h2>
</div>
<p>A common piece of advice offered to developers making a
transition from procedural to object-oriented code is that a class
should not model a function. Such classes are often named as
actions, and typically sport a principal or single member function
named &quot;do such and such&quot;. While this advice does guard against a
common pitfall, it is not always poor practice. Those that have
taken this rule of thumb to heart as a legalistic rule need to
unlearn a little to appreciate how objects can encapsulate tasks
and, in particular, mimic functions. The COMMAND pattern [<a href=
"#Gamma-1995">Gamma-1995</a>] demonstrates the power of task-based
objects. The FUNCTOR idiom [<a href="#Coplien1992">Coplien1992</a>]
focuses on functional objects that overload <tt class=
"methodname">operator()</tt> to achieve the appearance and
transparency of use of conventional functions.</p>
<p>The standard library provides for the use of function objects
with generic functions and templated containers, categorizing them
as unary or binary functions. It also defines specific function
object classes - e.g. <tt class="function">less</tt> for ordered
comparison - and function object adaptors - e.g. <tt class=
"function">pointer_to_unary_function</tt> to wrap up naked function
pointers. The Boost library [<a href="#Boost">Boost</a>] extends
this with other function object classes, adaptors, and the
<span class="emphasis"><em>nullary</em></span> function category,
for function objects taking no arguments.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e202" id="d0e202"></a>Re-member</h3>
</div>
<p>As an example of a function object class, focusing on the
nullary form for <tt class="type">void</tt> returning functions,
Listing 1 shows code for a member function adaptor. You may have
already come across the <tt class="classname">mem_fun_t</tt> family
of adaptors in the standard library. However, there are key
differences:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>A <tt class="classname">remember_function</tt> bundles a target
object together with a member function pointer for later callback
through nullary <tt class="methodname">operator()</tt>, whereas a
<tt class="classname">mem_fun_t</tt> object simply holds a member
function pointer and uses the argument to <tt class=
"methodname">operator()</tt> as its target.</p>
</li>
<li>
<p>Although it is of little practical consequence for a nullary,
voidreturning function, a variant for <tt class=
"literal">const</tt> member functions is not required because the
member pointer's type is parameterized as a whole.</p>
</li>
<li>
<p>The target pointer type need not be a raw pointer: smart
pointers supporting <tt class="methodname">operator-&gt;*</tt> will
also work.</p>
</li>
<li>
<p>The member pointer type need not be a member function pointer: a
member data pointer that points to a nullary function object will
also work.</p>
</li>
</ul>
</div>
<p>The <tt class="function">remember</tt> template function is a
helper that simplifies composition of <tt class=
"classname">remember_function</tt> objects, automatically deducing
the parameter types in the manner of <tt class=
"function">make_pair</tt> for <tt class="classname">pair</tt>,
<tt class="function">bind2nd</tt> for <tt class=
"classname">binder2nd</tt>, or <tt class="function">ptr_fun</tt>
for <tt class="classname">pointer_to_unary_function</tt> and
<tt class="classname">pointer_to_binary_function</tt>.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e273" id="d0e273"></a>Generalized
Function Pointer</h2>
</div>
<p>The need for event-driven callbacks, such as timer-triggered
actions, is often met with pointers to functions or an
implementation of OBSERVER [<a href="#Gamma-1995">Gamma-1995</a>].
The former approach is fine for simple event handlers:</p>
<pre class="programlisting">
class timer {
public:
  void set(const time &amp;delay,
  void (*callback)());
  ...
};
</pre>
<p>But it is inflexible, handling only functions and not context
objects. The OBSERVER-based solution introduces a base class that a
concrete handler class must implement:</p>
<div class="informaltable">
<table border="1" cellspacing="0">
<tr>
<td>
<pre class="programlisting">
template&lt;typename target_ptr_type,
         typename member_ptr_type&gt;
class remember_function {
public:
  remember_function(target_ptr_type on,
                  member_ptr_type call)
    : ptr(on), member(call) {}
  void operator()() const {
    (ptr-&gt;*member)();
  }
private:
  target_ptr_type ptr;
  member_ptr_type member;
};

template&lt;typename target_ptr_type,
         typename member_ptr_type&gt;
remember_function&lt;target_ptr_type,
                member_ptr_type&gt;
remember(target_ptr_type on,
        member_ptr_type call) {
  return remember_function&lt;target_ptr_type,
              member_ptr_type&gt;(on, call);
}
</pre>
<p><span class="bold"><b>Listing 1. Function object class and
helper for binding target object and member function
pointer.</b></span></p>
</td>
</tr>
</table>
</div>
<pre class="programlisting">
class handler {
public:
  virtual void run() = 0;
  ...
};
class timer {
public:
  void set(const time &amp;delay,
           handler *callback);
  ...
};
</pre>
<p>However, this introduces a level of indirection that leads to
additional memory management responsibilities, and imposes an
intrusive base class participation on users for what is a
relatively simple scenario. Using arbitrary objects or functions
for callback would be preferred. Overloading multiple <tt class=
"methodname">set</tt> member functions in <tt class=
"methodname">timer</tt> is a kitchen-sink solution, leading to a
wide interface that attempts to please all people and an awkward
<tt class="methodname">timer</tt> implementation.</p>
<p>Function objects at first appear to offer a route out: A nullary
function pointer or object could be passed in, including a
<tt class="function">remember_function</tt> binding of member to
target, and later called back. A member template function would
accommodate the substitutability of all the variations:</p>
<pre class="programlisting">
class timer {
public:
  template&lt;typename nullary_function&gt;
  void set(const time &amp;delay,
           nullary_function callback);
  ...
};
</pre>
<p>However, this raises a fundamental problem: How does a
<tt class="classname">timer</tt> object later execute the <i class=
"parameter"><tt>callback</tt></i> passed in? Unlike many examples
of member template functions in the standard library, this one does
not execute the function or function object immediately - it would
not be much of a timer if it did! The <tt class=
"classname">timer</tt> needs to store the callback for later use.
Without parameterizing the whole <tt class="classname">timer</tt>
class on the <tt class="function">nullary_function</tt> type,
rather than just the <tt class="methodname">set</tt> member, this
does not appear to be possible. Templating the whole class is
undesirable because it means that for each different type of
callback, a different timer class instantiation is needed.</p>
<p>A further problem with the member template approach is that a
member template function cannot be declared <tt class=
"literal">virtual</tt>. This would be significant if the <tt class=
"classname">timer</tt> class were an abstract rather than a
concrete class, i.e. an interface to timer features rather than a
single implementation. The attempt to decouple both the mechanism
of the timer and the target type like this would lead to the
following illegal code:</p>
<pre class="programlisting">
class timer
{
public:
  template&lt;typename nullary_function&gt;
                             // illegal
  virtual void set(const time &amp;delay,
                   nullary_function callback) = 0;
  ...
};
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e345" id="d0e345"></a>On the
Outside</h2>
</div>
<p>It is possible to resolve the tension in the design by
approaching it from a different angle. We can take a step back and
ask what simple interface to <tt class="classname">timer</tt> would
also simplify its implementation. What is needed is some kind of
abstraction of a function pointer that is both generic and generic:
generic in the sense of supporting the generic programming style of
the STL, and generic in the sense that it is general purpose and
easily used in any context:</p>
<pre class="programlisting">
class timer {
public:
  void set(const time &amp;delay,
           const function_ptr &amp;callback);
  ...
};
</pre>
<p>To satisfy the requirements for simplicity in <tt class=
"classname">timer</tt> and our expectations of a function pointer,
<i class="parameter"><tt>function_ptr</tt></i> needs to support
syntax for initialization, assignment, and execution. Listing 2
shows such an interface.</p>
<div class="informaltable">
<table border="1" cellspacing="0">
<tr>
<td>
<pre class="programlisting">
class function_ptr {
public:
  function_ptr();
  function_ptr(const function_ptr &amp;other);
  template&lt;typename nullary_function&gt;
    function_ptr(nullary_function function);
  ~function_ptr();
  function_ptr &amp;operator=(
                const function_ptr &amp;rhs)
  void operator()() const;
  ...
};
</pre>
<p><span class="bold"><b>Listing 2. Smart function pointer
interface.</b></span></p>
</td>
</tr>
</table>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e373" id="d0e373"></a>On the
Inside</h2>
</div>
<p>This is all very well, but it has yet to solve the problem
fully: It looks nice, but how is it implemented? How can a
<tt class="classname">function_ptr</tt> object hold arbitrary
representation, constrained only by the requirement that it must
support an <tt class="methodname">operator()</tt> with no
arguments? The technique used is based on the EXTERNAL POLYMORPHISM
pattern [<a href="#Cleeland-1998">Cleeland-1998</a>], in particular
the use of inheritance and runtime polymorphism to adapt
template-based genericity for value-based objects through a level
of indirection [<a href="#Henney2000b">Henney2000b</a>]. Listing 3
opens up <tt class="classname">function_ptr</tt> to show this
collaboration in practice, including the conversion (i.e.
initialization) from any arbitrary nullary function object or
pointer.</p>
<div class="informaltable">
<table border="1" cellspacing="0">
<tr>
<td>
<pre class="programlisting">
class function_ptr {
public:
  function_ptr()
    : body(0) {}
  template&lt;typename nullary_function&gt;
  function_ptr(nullary_function function)
    : body(new adaptor&lt;nullary_function&gt;
                             (function)) {}
  ~function_ptr() {
    delete body;
  }
  ...
private:
  class callable {
  public:
    virtual ~callable() {}
    virtual callable *clone() const = 0;
    virtual void call() = 0;
  };
  template&lt;typename nullary_function&gt;
  class adaptor : public callable {
  public:
    adaptor(nullary_function function)
      : adaptee(function) {}
    virtual callable *clone() const {
      return new adaptor(adaptee);
    }
    virtual void call() {
      adaptee();
    }
    nullary_function adaptee;
  };
  callable *body;
};
</pre>
<p><span class="bold"><b>Listing 3. Smart function pointer
representation and basic construction.</b></span></p>
</td>
</tr>
</table>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e403" id="d0e403"></a>Clone Me</h2>
</div>
<p><tt class="classname">function_ptr</tt> is a value type, so it
stands to reason that it should support copying through
construction and assignment - an identity form of inward conversion
[<a href="#Henney2000b">Henney2000b</a>]. The body of a <tt class=
"classname">function_ptr</tt> cannot be copied directly because of
the decoupling of interface from implementation, which leads to the
polymorphic copying, or cloning, technique shown in Listing 4.</p>
<div class="informaltable">
<table border="1" cellspacing="0">
<tr>
<td>
<pre class="programlisting">
class function_ptr {
public:
  ...
  function_ptr(const function_ptr &amp;other)
    : body(other.body
             ? other.body-&gt;clone()
             : 0) {}
  function_ptr &amp;operator=(
              const function_ptr &amp;rhs) {
    callable *old_body = body;
    body = rhs.body
             ? rhs.body-&gt;clone()
             : 0;
    delete old_body;
    return *this;
  }
  ...
};
</pre>
<p><span class="bold"><b>Listing 4. Smart function pointer
copying.</b></span></p>
</td>
</tr>
</table>
</div>
<p>The assignment operator uses the COPY BEFORE RELEASE idiom
[<a href="#Henney1998">Henney1998</a>] for exception- and
self-assignment-safety. A nonthrowing <tt class=
"function">swap</tt> could also be used for this [<a href=
"#Sutter2000">Sutter2000</a>], but for this article the interface
to <tt class="classname">function_ptr</tt> is being kept small and
based only on operators.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e440" id="d0e440"></a>Call Me</h2>
</div>
<p>The final piece of the jigsaw is to dereference a <tt class=
"classname">function_ptr</tt> - fetch and execute. A raw function
pointer supports dereferencing through <tt class=
"methodname">operator*</tt>, which is the identity operation on a
function pointer, <tt class="methodname">operator()</tt>, which can
be called directly on a function pointer without using <tt class=
"methodname">operator*</tt>, but no <tt class=
"methodname">operator-&gt;</tt>. This is the model that <tt class=
"classname">function_ptr</tt> should follow, and does so in Listing
5. For a null pointer, the execution assumes that for no function
there is no function, as opposed to undefined behaviour as per
built-in pointers.</p>
<div class="informaltable">
<table border="1" cellspacing="0">
<tr>
<td>
<pre class="programlisting">
class function_ptr {
public:
  ...
  void operator()() const {
  if(body)
    body-&gt;call();
  }
  function_ptr &amp;operator*() {
    return *this;
  }
  const function_ptr &amp;operator*() const {
    return *this;
  }
  ...
};
</pre>
<p><span class="bold"><b>Listing 5. Smart function pointer
dereferencing and calling.</b></span></p>
</td>
</tr>
</table>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e473" id="d0e473"></a>Remember
Me?</h2>
</div>
<p>A focus on forms of substitutability - in this case derivation,
overloading, and templates, each a way of establishing an interface
- can decouple a system, allowing greater suppleness and clearer
code. Putting it all together, we can put together a simple
scenario based around the proposed <tt class="classname">timer</tt>
interface. Consider the interface to a device that can be turned on
or off at particular times, e.g. a heating or an air conditioning
system:</p>
<pre class="programlisting">
class device {
public:
  virtual void turn_on() = 0;
  virtual void turn_off() = 0;
  ...
};
</pre>
<p>The following example combines the concepts and features
presented so far:</p>
<pre class="programlisting">
void set_up(device *target, timer *scheduler,
  const time &amp;on, const time &amp;off) {
    scheduler-&gt;set(on,
          remember(target, &amp;device::turn_on));
    scheduler-&gt;set(off,
          remember(target, &amp;device::turn_off));
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e487" id=
"d0e487"></a>Conclusion</h2>
</div>
<p>There is little that excites programmers' passions more than
discussions of style, but there is little that helps them more than
common understanding. Overloading is a powerful feature whose
reasoned use underpins many idioms at the heart of the modern C++
programmer's vocabulary: smart pointers, function objects,
iterators, etc.</p>
<p>Within each of these idioms there is variation for expression
rather than any simplistic, one-size-fits-all, cookie-cutter rule.
A function object will support some form of <tt class=
"methodname">operator()</tt>, and a smart pointer must support some
form of dereferencing, but this does not by necessity include
<tt class="methodname">operator-&gt;</tt>, as demonstrated by
<tt class="classname">function_ptr</tt>, a smart function
pointer.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e503" id="d0e503"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Boost" id="Boost"></a>
<p class="bibliomixed">[Boost] Boost library website, <span class=
"bibliomisc"><a href="http://www.boost.org" target=
"_top">http://www.boost.org</a></span>.</p>
</div>
<div class="bibliomixed"><a name="Cleeland-1998" id=
"Cleeland-1998"></a>
<p class="bibliomixed">[Cleeland-1998] Chris Cleeland, Douglas C
Schmidt, and Tim Harrison, &quot;External Polymorphism&quot;, <span class=
"citetitle"><i class="citetitle">Pattern Languages of Program
Design 3</i></span>, edited by Robert Martin, Dirk Riehle, and
Frank Buschmann, Addison-Wesley, 1998.</p>
</div>
<div class="bibliomixed"><a name="Coplien1992" id=
"Coplien1992"></a>
<p class="bibliomixed">[Coplien1992] James O Coplien, <span class=
"citetitle"><i class="citetitle">Advanced C++: Programming Styles
and Idioms</i></span>, Addison-Wesley, 1992.</p>
</div>
<div class="bibliomixed"><a name="Gamma-1995" id="Gamma-1995"></a>
<p class="bibliomixed">[Gamma-1995] Erich Gamma, Richard Helm,
Ralph Johnson, and John Vlissides, <span class=
"citetitle"><i class="citetitle">Design Patterns: Elements of
Reusable Object-Oriented Software</i></span>, Addison-Wesley,
1995.</p>
</div>
<div class="bibliomixed"><a name="Henney1998" id="Henney1998"></a>
<p class="bibliomixed">[Henney1998] Kevlin Henney, &quot;Creating Stable
Assignments&quot;, <span class="citetitle"><i class="citetitle">C++
Report</i></span> 10(6), June 1998, also available from
<span class="bibliomisc"><a href="http://www.curbralan.com" target=
"_top">http://www.curbralan.com</a></span>.</p>
</div>
<div class="bibliomixed"><a name="Henney2000a" id=
"Henney2000a"></a>
<p class="bibliomixed">[Henney2000a] Kevlin Henney, &quot;From Mechanism
to Method: Substitutability&quot;, <span class="citetitle"><i class=
"citetitle">C++ Report</i></span> 12(5), May 2000, also available
from <span class="bibliomisc"><a href="http://www.curbralan.com"
target="_top">http://www.curbralan.com</a></span>.</p>
</div>
<div class="bibliomixed"><a name="Henney2000b" id=
"Henney2000b"></a>
<p class="bibliomixed">[Henney2000b] Kevlin Henney, &quot;From Mechanism
to Method: Valued Conversions&quot;, <span class="citetitle"><i class=
"citetitle">C++ Report</i></span> 12(7), May 2000, also available
from <span class="bibliomisc"><a href="http://www.curbralan.com"
target="_top">http://www.curbralan.com</a></span>.</p>
</div>
<div class="bibliomixed"><a name="Liskov1987" id="Liskov1987"></a>
<p class="bibliomixed">[Liskov1987] Barbara Liskov, &quot;Data
Abstraction and Hierarchy&quot;, <span class="citetitle"><i class=
"citetitle">OOPSLA '87 Addendum to the Proceedings</i></span>,
October 1987.</p>
</div>
<div class="bibliomixed"><a name="Meyers1996" id="Meyers1996"></a>
<p class="bibliomixed">[Meyers1996] Scott Meyers, <span class=
"citetitle"><i class="citetitle">More Effective C++: 35 New Ways to
Improve Your Programs and Designs</i></span>, Addison-Wesley,
1996.</p>
</div>
<div class="bibliomixed"><a name="Meyers1999" id="Meyers1999"></a>
<p class="bibliomixed">[Meyers1999] Scott Meyers, &quot;Implementing
operator-&gt;* for Smart Pointers&quot;, <span class=
"citetitle"><i class="citetitle">Dr. Dobb's Journal</i></span>,
October 1999.</p>
</div>
<div class="bibliomixed"><a name="Strunk-1979" id=
"Strunk-1979"></a>
<p class="bibliomixed">[Strunk-1979] William Strunk Jr and E B
White, <span class="citetitle"><i class="citetitle">The Elements of
Style</i></span>, 3rd edition, Macmillan, 1979.</p>
</div>
<div class="bibliomixed"><a name="Sutter2000" id="Sutter2000"></a>
<p class="bibliomixed">[Sutter2000] Herb Sutter, <span class=
"citetitle"><i class="citetitle">Exceptional C++</i></span>,
Addison-Wesley, 2000.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
