You cannot have an auto
parameter. You basically have two options:
Option #1: Use std::function
as you have shown.
Option #2: Use a template parameter:
template<typename F>
void f(F &lambda) { /* ... */}
Option #2 may, in some cases, be more efficient, as it can avoid a potential heap allocation for the embedded lambda function object, but is only possible if f
can be placed in a header as a template function. It may also increase compile times and I-cache footprint, as can any template. Note that it may have no effect as well, as if the lambda function object is small enough it may be represented inline in the std::function
object.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…