Overloading for_each for specific iterator types
<p>I'm using a typedef to define the type of a container in my program so that I can easily switch between using normal STL containers and <a href="http://stxxl.sourceforge.net/" rel="nofollow">STXXL</a> containers, along the lines of:</p>

<pre><code>typedef stxxl:vector&lt;Data&gt; MyContainer;
</code></pre>

<p>or</p>

<pre><code>typedef std:vector&lt;Data&gt; MyContainer;
</code></pre>

<p>One difficulty is that STXXL provides a special version of <code>std::for_each</code>, <code>stxxl::for_each</code> that is optimised for use with STXXL containers. I'd prefer to use this function when MyContainer is typedeffed as a <code>stxxl::vector</code>.</p>

<p>One solution would be to define my own <code>for_each</code> function that calls the right <code>for_each</code> function and use that whenever I want to call <code>for_each</code>. </p>

<p>Another solution that I'm currently investigating is to overload/specialize <code>std::foreach</code> so that it calls <code>stxxl::for_each</code> whenever it is called with a <code>stxxl::vector&lt;Data&gt;::(const_)iterator</code> as first and second argument.</p>

<p>I cannot get the second idea to work though. I've tried the following:</p>

<pre><code>namespace std
{
    template &lt;class UnaryFunction&gt;
    UnaryFunction for_each(stxxl:vector&lt;Data&gt;::const_iterator first, 
        stxxl:vector&lt;Data&gt;::const_iterator last, UnaryFunction f)
    {
        stxxl::for_each(first, last, f, 4);
    }
}
</code></pre>

<p>Along with a similar function for non-const iterators. They don't get called though.</p>

<p>What would be the preferred solution to this problem? How can I get my version of <code>std::for_each</code> for <code>stxxl::vector</code> iterators to get called?</p>

<p><strong>Update:</strong> I got the second idea to work now, as posted. The problem was that I was including the wrong file (ouch...). The first question remains though: What is the preferred solution to this problem? Is it okay to overload std::for_each, as the std namespace is not intended for mere mortals?</p>
