Basically, like lambdas, common lambdas are defined by means of equivalence to an implicitly defined function object (where non-trivial lambdas have an additional ability, which can be converted to a function pointer with an empty capture). The only real difference between generic and non-trivial lambdas is that the common lambda has a call operator, which is a function template, while it is not a template for non-generic lambdas.
- For generic lambdas, conversion to function pointers does not exist (see 5.1.2 [expr.prim.lambda], clause 6).
- Since generic lambdas are still objects with a call operator, they can be used directly as an argument where common function functions can be used. This does not apply to function templates: they behave more like a set of overloads, and you need to get an instance before you can pass them as an object of a function.
- Although you cannot get a pointer to a function template, you can get a pointer to a function template specialization (as @Columbo noted in a comment, function template specialization is a function). You cannot get a function pointer from a common lambda.
Function templates are involved in resolving overloads, while functional objects really are not involved: when an object is in the search for a name, this object is selected even if functions with the same name can be detected and with good correspondence with overloading. This means that these two equivalents are not :
template <typename T> auto identity(T value) { return value; } auto identity = [](auto value) { return value; }
The second implementation captures the name, and the first is used as a candidate when resolving overloads.
Besides the helper objects created inside the function, I would use common lambda for functions that are not intended for setting points (for example, it would be nice if such library objects were standard library algorithms). The main advantage is that functional objects can be easily adapted, for example, using std::bind() , which does not match function templates.
source share