A lambda expression that has an empty capture list (capture-less) can be converted to a function pointer:
void func(void(*fp)(int)) {
fp(100);
}
// captureless lambda as 'void(*)(int)'
func([](int x){ //OK
// use x
});
A lambda expression that has at least one parameter of type auto
is called a generic lambda expression. A generic lambda is equivalent to a function object with a template function-call operator. Following code shows an example of a generic lambda and its equivalent function object type:
auto foo = [](auto x) { /*body*/ };
foo(10); //OK
foo(100.5); //OK
// foo's type is equivalaent to
struct fooType {
....
template<typename T>
void operator()(T x) const { /*body*/ }
....
};
Due to a templatized function-call operator, a capture-less generic lambda is potentially convertible to multiple function pointer types, depending on its arguments. This question is associated with the nextptr article "Generic code with generic lambda expression", which has a short description of generic lambda's features and benefits.
Consider a capture-less generic lambda function square below:
auto square = [](auto x) {
return x*x;
};
Select all the function pointer types below to which the lambda square can be converted. To answer this question, you should take integral promotions into consideration. Please check the Explanations
for details.