Can I use this work around to pass locally defined functor to algorithim functions?
<p>I'm not using C++11 or Boost. I want to use functors and pass to algorithms such as std::for_each, but I consider it too messy to have to define the functors outside the function. I want to define them locally in the function just before they are used. However, the following does not work. This is due to old C++ Standard, that does NOT allow locally defined classes to be used as template parameter (fixed in C++11).</p>

<pre><code>int main()
{
    std::vector&lt;int&gt; v(10);

    class SetInc
    {
    public:
        SetInc() : x(0) {}

        virtual void operator () (int&amp; a)
        {
            a = x++;
        }

    private:
        int x;
    } f;

    std::for_each(v.begin(), v.end(), f);

    std::copy(v.begin(), v.end(), std::ostream_iterator&lt;int&gt;(std::cout, "\n"));
}
</code></pre>

<p>But I've developed the following work around:</p>

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

template &lt;typename ARGUEMENT, typename RESULT&gt;
class FunctorBase
{
public:
    typedef ARGUEMENT argument_type;
    typedef RESULT result_type;

    virtual result_type operator () (argument_type) = 0;
    FunctorBase() {}
    virtual ~FunctorBase() {}
};

template &lt;typename ARGUEMENT, typename RESULT&gt;
class FunctorWrapper
{
public:
    typedef ARGUEMENT argument_type;
    typedef RESULT result_type;
    typedef FunctorBase&lt;argument_type, result_type&gt; Functor_T;

    explicit FunctorWrapper(Functor_T *functor)
        : functor(functor)
    {}

    result_type operator () (argument_type a)
    {
        return (*functor)(a);
    }

private:
    Functor_T *functor;
};

template &lt;typename ARGUEMENT, typename RESULT&gt;
FunctorWrapper&lt;ARGUEMENT, RESULT&gt; make_unary_functor(FunctorBase&lt;ARGUEMENT, RESULT&gt;&amp; f)
{
    return FunctorWrapper&lt;ARGUEMENT, RESULT&gt;(&amp;f);
}

int main()
{
    std::vector&lt;int&gt; v(10);

    class SetInc : public FunctorBase&lt;int&amp;, void&gt;
    {
    public:
        SetInc() : x(0) {}

        virtual result_type operator () (argument_type a)
        {
            a = x++;
        }

    private:
        int x;
    } f;

    std::for_each(v.begin(), v.end(), make_unary_functor(f));

    std::copy(v.begin(), v.end(), std::ostream_iterator&lt;int&gt;(std::cout, "\n"));
}
</code></pre>

<p>Is that good?</p>
