How to avoid code duplication in this example?
<p>I have two classes, <code>Base</code> and <code>Derived</code>. I provide a function that applies a function to all elements of <code>Base</code>, and another function to apply a function to all elements of <code>Derived</code>:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

class Base {
  typedef std::vector&lt;int&gt; Vec;
  typedef Vec::const_iterator CIter;
  CIter begin() const;
  CIter end() const;
};

class Derived : public Base {
  void add(int);
};


template&lt;class F&gt;
void processBase(const Base&amp; b, F&amp; f) {
  for (Base::CIter it = b.begin(); it != b.end(); ++it) {
    f(b, *it);
  }
}

template&lt;class F&gt;
void processDerived(Derived* d, F&amp; f) {
  for (Base::CIter it = d-&gt;begin(); it != d-&gt;end(); ++it) {
    f(d, *it);
  }
}
</code></pre>

<p>How can I make the one function call the other, so that I don't have code duplication? (I gave only a minimal example; in my real code the <code>for</code> loops are kind of complicated, and that code is exactly repeated in the two functions.)</p>

<p>Do I have to template the <code>processBase</code> function? Or is there a way to use casting?</p>
