    <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  :: Thoughts on Functoids</title>
        <link>https://members.accu.org/index.php/articles/561</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 #31 - Apr 1999</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/c173/">31</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+173/">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;Thoughts on Functoids</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 April 1999 17:50:52 +01:00 or Mon, 26 April 1999 17:50:52 +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>Hi Francis,</p>
<p>I have a question that you might be interested in, or at least
have an opinion on, concerning function objects. I've elaborated a
little so that if might make more sense to readers of different
levels if you put it in C Vu.</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>I am fairly certain that this level of C++ belongs in Overload.
So I have added some thoughts of mine and hope that readers will
feel able to comment (in my opinion, readers should be adding their
contribution, without that there is very little to motivate people
like Richard to take the time to share their speculations)
Francis</p>
</blockquote>
</div>
<p>It seems to me that the traditional method of using function
objects is to define a templatised processing function that accepts
any type of function object as a parameter. The function objects
cannot be passed in directly by value, because that might cause
them to be sliced, and also would cause them to be used as whatever
the compile-time type of the parameter was declared as. The use of
template parameters ensures that the actual object type makes it
all the way in to the implementation of the processing
function.</p>
<p>In other words, define a template processing function to use
whatever type of function object is passed in:</p>
<pre class="programlisting">
// This function processes input based on op,
// and returns the result. &quot;data&quot; is some 
// data type.
template&lt;typename Operation&gt;
data process(const data &amp;input, Operation op);
</pre>
<p>Note that this is how the sort method is defined in my version
of the library:</p>
<pre class="programlisting">
template&lt;class RanIt, class Pred&gt;
void sort(RanIt first, RanIt last, Pred pr);
</pre>
<p>The predicate function object, <tt class="classname">pr</tt>, is
passed by templatised value. The template function merely assumes
that <tt class="classname">pr</tt> supports an appropriate
<tt class="methodname">operator()(...)</tt> call, and uses it. In
the case of sort, I believe it assumes that pr supports an
<tt class="methodname">operator()</tt> method with the following
signature:</p>
<pre class="programlisting">
// Compare the left and right objects and
// return true if you want &quot;left &lt; right&quot;
bool operator()(const T &amp;left,
                const T &amp;right) const;
</pre>
<p>Now, in my current project I could potentially have an awful lot
of different function objects. It is also possible that the
processing function itself might not be completely trivial but may
have some substantial body to it. The problem with using templates
as shown above is that for every function object that is used, a
new copy of the <tt class="methodname">process()</tt> method may
well be instantiated, which is able to cope with the particular
function object, whatever its size, and which will call the correct
<tt class="methodname">operator()(...)</tt> method. I wonder
whether you can think of any significant arguments against the
following alternative scheme:</p>
<p>Define a base class for all the function objects with a (pure)
<tt class="methodname">virtual operator()(...)</tt> method. Then
use pass by reference (or pointer) in the <tt class=
"methodname">process()</tt> function to ensure that the appropriate
function object method is called at runtime.</p>
<pre class="programlisting">
// Define base class with pure virtual
// operator() method. I derive from the 
// standard library &quot;unary_function&quot; template
// class here for consistency.
class operation :
  public std::unary_function&lt;data, data&gt;
{
public:
  // This is my overloaded operator() method
  virtual data
  operator()(const data &amp;input) const = 0;
};

// I would now derive all my real operations
// from the above class, and define the
// virtual operator() method for each.

// The process() method is no longer a
// template, but uses polymorphism to ensure
// operator() is called on the correct
// function object.
data process(const data &amp;input,
             const operation &amp;op);
</pre>
<p>I guess you get an extra pointer dereference to make the virtual
call, but I am not worried about that level of detail at this stage
of development. It would seem that this second method should remove
any chance of code bloat from multiple instantiations of the
processing function, because the function is now no longer
templatised and so you'll just get the one copy. This may be a
completely obvious thing to do, but I have only ever seen the
previous template solution being used and so I am suffering from
shock from the unfamiliar. It seems to work OK in my prototype,
however!</p>
<p>I can see why the template method was chosen for the standard
library functions. You can choose to go the second route if you
wish to, because you have to instantiate even the first copy of
<tt class="function">sort&lt;&gt;()</tt> in your code. Avoidance of
the overhead of virtual functions in the standard (template)
library was for the best.</p>
<p>Cheers, Richard</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e80" id="d0e80"></a>Francis
Glassborow Comments&hellip;</h2>
</div>
<p>I am sure that Richard is an experienced user of function
objects (functoids) but some of the above is likely to confuse less
experienced programmers. So let me take a little time to try to
clarify several issues.</p>
<p>First of all those STL functions that take a predicate simply
require something that will be meaningful if a pair of parentheses
is added (possible containing some arguments that depend on the
other parameters. There are two kinds of C++ entity that can meet
this need. The simpler one is a simply the address of an ordinary
function. So, purely as an example:</p>
<pre class="programlisting">
class Rational {
  // suitable interface, including a
  // compare function &gt;
};
int compareRational (Rational const &amp; lhs,
                     Rational const &amp; rhs)
{
  return lhs.(rhs);
}
// I know there is an STL template to do this
// but I do not want to complicate the issue
void initRational(Rational &amp;)
{
  // initialises a default Rational somehow
}

void printRational(Rational const &amp;);

int main (){
  Rational array[10];
  for_each(array, array+10, initRational);
  sort(array, array+10, compareRational);
  for_each(array, array+10, printRational);
};
</pre>
<p>In the above the two calls to for_each and the call of sort
relies on passing the address of a global function as the third
argument. In the context of the code that is perfectly sensible.
However it is sometimes necessary to provide extra data at call
time. In other words the process you want to run relies on data
that would not be provided by the template parameters. For example
you might want to select an output stream for printing. In such
circumstances we need a way to wrap up that extra information. The
way this is done is to have a functoid whose constructor can tuck
the required data away as member data. Something along these
lines:</p>
<pre class="programlisting">
class printRational {
  ostream &amp; out_m;
public:
  printRational(ostream &amp; out = cout)
  : out_m(out){};
  operator () (Rational const &amp;);
};
</pre>
<p>Now I can write:</p>
<pre class="programlisting">
for_each(array, array+10, printRational() );
</pre>
<p>Note that extra set of parentheses. They are there because a
temporary (unnamed) printRational object has to be created - using
the default constructor this time. When the temporary is bound to
the pred parameter of for_each it works like a function call
because writing pred(&lt;specific Rational object&gt;) makes
perfect sense to the compiler. However functoids must be
constructed. The idea of using functoids antedates templates by
many years. The manipulators that you use with iostream objects are
functoids (well mostly, though those that take no parameters are
plain functions). The concept that functoids are specifically for
template use is seriously faulty. Another place they can be used is
in implementing the visitor pattern. Indeed anytime you need to
pass a function like object but one that needs extra data bound to
it at call time a functoid is going to be a prime candidate. They
are a very powerful tool in the hands of the experienced C++
programmer and if you do not have a sound understanding of them you
need to mark that as a priority area in which to develop your
skills.</p>
<p>Now let me turn to the specific problem of functoids used in the
context of templates. They are certainly an area in which code
bloat is a potential problem. I say potential because in many cases
this does not happen. A good compiler should be able to expand
for_each inline to produce code as efficient as that produced by
hand.</p>
<p>There is a general guideline with templates, always place code
that is independent of the template parameters in its own
non-template class. This class maybe used as a base for the
template or an instance maybe a data member of the template.
However, as with all optimisation, programmers must be very sure
they understand what they are doing.</p>
<p>Richard's suggestion has several penalties. First it requires
the predicates be functoids and not pure functions. Then there is
the issue of bloat because of virtual inheritance. Every one of
Richard's derived functoids will carry around a virtual function
table. In addition there is the cost of writing and maintaining the
template specialisations that will use his functoid hierarchies.
There are also the overheads involved in dynamic binding to use his
derived functoids.</p>
<p>I suspect, but do not know, that the combination of the above
will produce similar code bloat. I think that the programming
effort might be better placed ensuring that good template code is
written with all independent code placed elsewhere.</p>
<p>I am sure we would all welcome further comments on this. I know
I would.</p>
<p>Francis Glassborow</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
