A point from n3290 ISO draft:
Lambda expressions : section 5.1.2, para 6:
"The closure type for a lambda-expression with no
lambda-capture has a public non-virtual non-explicit const
conversion function to pointer to function having the same
parameter and return types as the closure type’s function
call operator. The value returned by this conversion
function shall be the address of a function that, when
invoked, has the same effect as invoking the closure
type’s function call operator."
Can any one explain this point with an example please ?
The short answer
This just means that lambdas not capturing anything can be converted into a function pointer with the same signature:
And a capture makes it illegal:
The long answer
Lambdas are shorthand to create a functor:
Is equivalent to:
In this case
func_typeis the “closure type”and operator()is the “function call operator”. When you take the address of a lambda, it is as if you declared theoperator()static and take its address, like any other function:When you have captured variables, they become members of
func_type.operator()depends on these members, so it can’t be made static:An ordinary function has no notion of member variables. Keeping with this thought, lambdas can only be treated as an ordinary function if they also have no member variables.