Assume that I have a function that abstracts some error-handling logic, it has argument which is a functor. Inside it, it calls this functor conditionally.
The function looks like this:
void HandleError(int err_code, std::function<void()> error_logger) {
//...
if (condition) {
error_logger();
}
//...
}
But now a caller needs to construct the error_logger (consider a lambda) before calling this function. It is a little bit not optimal.
How to lazily instantiate the error_logger only when it's called when the condition is true?
Unfortunately, I can only use C++14. But boost library could be used.
You can remove std::function from interface, for example you want to guarantee, that there will be no heap memory allocation, you can write the code in this way, using the lambdas directly
However there is no much sense to save the performance on std::function construction. Also there is an optimization, when the heap allocation is avoided (for ex. for gcc).