C++11 factory pattern with lambdas Anonymous functions are one of the best features of C++11. They make everything so beautiful! 

However, one can get carried away and start overusing them.

This code calls a function that reads through a file and invokes a callback everytime that something matches a regex. Since I read two different files but I want callbacks only to differ for a constant, I chose this "lambda-returning-lambda" factory pattern.

auto callback_factory = 
    [&amp;] (IPMarkerType start, IPMarkerType stop) -&gt; std::function&lt;void(IPNode&lt;T&gt;&amp;)&gt;
{
    return [&amp;,start,stop](IPNode&lt;T&gt; &amp; node){ 
        markers.push_back(IPMarker&lt;T&gt;(node.ip, start));  
        markers.push_back(IPMarker&lt;T&gt;(node.ip.network_ones(node.prefix), stop));
    };
};

read_regexp&lt;T&gt;("C:\\path\\to\\file1.txt", regex, callback_factory(ipm_a_open,ipm_a_close));
read_regexp&lt;T&gt;("C:\\path\\to\\file2.txt", regex, callback_factory(ipm_b_open,ipm_b_close));


Callbacks are really local just to the calling function, so I think that external functor would not be justified. On the other hand, this code smells a lot to me, so I would like to hear your comments.
First of all, I will assume there is something like template &lt;typename T&gt; somewhere in your code. Otherwise, I doubt it would compile.

There is something you could do to improve your program: use emplace_back instead of push_back with an object construction. That way, objects will be created right in the collection (list, vector, etc...). By the way, I'll also assume that markers is a standard collection.

auto callback_factory = 
    [&amp;] (IPMarkerType start, IPMarkerType stop) -&gt; std::function&lt;void(IPNode&lt;T&gt;&amp;)&gt;
{
    return [&amp;,start,stop](IPNode&lt;T&gt; &amp; node){ 
        markers.emplace_back(node.ip, start);  
        markers.emplace_back(node.ip.network_ones(node.prefix), stop);
    };
};


And, yeah, something still smells: I really wonder how and/or where you declared markers. If it's a global variable, then I think the template parameter may cause some problems.