Linked lists of templated classes vs function ptr I am designing a library for a hierarchical problem. At each stage of the hierarchy there are several "tools" at hand. If at a state N one tool succeeds, the solution is passed to the next (N+1) state. If all tools of stage N fail, we return to the state N-1 (previous) and let the previous stage handle the problem. 

I have two possible ideas how to structure this problem. One uses a linked list of templated classes, the other binds directly to the next stage's run function. 
My question is, is any of these superior to the other (or are both flawed) and why? 

The first solution uses templates to create a linked list of templated classes.

template &lt;typename Input, typename Output&gt;
struct BaseSolver
{
    virtual boost::optional&lt;Output&gt; run(const Input&amp; _input) = 0;
};

template&lt;typename Input&gt;
struct BaseStage
{
    virtual bool runAndRecover(const Input &amp;_input)
    {
        return true;
    }
};

template&lt;typename Input, typename Output&gt;
struct Stage1 : BaseStage&lt;Input&gt;
{
    bool run(const Input&amp; _input)
    {
        for(const auto&amp; solver : solvers_)
        {
            const auto currentResult = solver-&gt;run(_input);
            if(currentResult &amp;&amp; nextStage-&gt;runAndRecover(currentResult.get()))
            {
                return true;
            }
        }
        return false;
    }

    std::vector&lt;boost::shared_ptr&lt;BaseSolver&lt;Input, Output&gt;&gt;&gt; solvers_;

    BaseStage&lt;Output&gt;* nextStage;
};


This is the other solution. It binds only to the run() function of the next stage 

template&lt;typename Input, typename Output&gt;
struct Stage2
{
    typedef boost::function&lt;boost::optional&lt;Output&gt; (Input)&gt; SolverFunction;
    typedef boost::function&lt;bool(Output)&gt; NextStage;

    bool run(const Input&amp; _input)
    {
        for(const auto&amp; solver : solverFunctions_)
        {
            const auto solverResult = solver(_input);
            if(solverResult &amp;&amp; nextStage_(solverResult.get()))
            {
                return true;
            }
        }
        return false;
    }

    std::vector&lt;SolverFunction&gt; solverFunctions_;
    // will be bind to the next stage::run
    NextStage nextStage_;
};


PS: Both versions compile and run as a prototype.
The second is simpler, neater and avoids virtual functions. Prefer std::function over boost::function unless the latter gives you something extra, which it doesn't here