Check out EMC++ (Effective Modern C++) by Scott Meyers.// The best-practices book for C++11 and C++14 :-)In particular, quoting selectively from Item 5: Prefer `auto` to explicit type declarations:QuoteIt's important to recognize that even setting aside the syntactic verbosity and need to repeat the parameter types, using std::function is not the same as using auto. An auto-declared variable holding a closure has the same type as the closure, and as such it uses only as much memory as the closure requires. The type of a std::function-declared variable holding a closure is an instantiation of the std::function template, and that has a fixed size for any given signature. This size may not be adequate for the closure it's asked to store, and when that's the case, the std::function constructor will allocate heap memory to store the closure. The result is that the std::function object typically uses more memory than the auto-declared object.And, thanks to implementation details that restrict inlining and yield indirect function calls, invoking a closure via a std::function object is almost certain to be slower than calling it via an auto-declared object. In other words, the std::function approach is generally bigger and slower than the auto approach, and it may yield out-of-memory exceptions, too. Plus, as you can see in the examples above, writing `auto` is a whole lot less work than writing the type of the std::function instantiation. In the competition between auto and std::function for holding a closure, it's pretty much game, set, and match for auto. (A similar argument can be made for auto over std::function for holding the result of calls to std::bind, but in Item 34, I do my best to convince you to use lambdas instead of std::bind, anyway.)It's important not to overlook the (aforementioned) exception safety implications! Note: http://en.cppreference.com/w/cpp/utilit ... ptionsThis
has costs beyond performance (user-friendliness / complexity, maintenance)...Item 34: Prefer lambdas to std::bind is worth a look, too!