Have a look at the code example listed below, I tested it with Compiler explorer (using gcc and clang) and it works and prints out the (expected) output of 200.
What I am trying to figure out: why exactly is this valid C++. Or is it not?
Here, I'm using the using
keyword to define an alias (instead of a typedef
) for the function type ft
, which describes functions that take an int
as argument and return an int
(ft
is a function type, not a function pointer type!). The equivalent typedef
syntax is typedef int ft(int);
. Since the C++ standard says that for every typedef
there is an equivalent form using the using
syntax, it is clear that using ft = int(int)
is a well-defined way to specify this function type.
Now comes the interesting part: I use the type ft
to specify the argument type of another function (print_it
), and then call it, passing a lambda function. My question is: where/how does the C++ standard say that this should actually work? I think it is a wonderfully simple way to pass lambdas around. That this works is not clear to me, as a lambda is really a functor, not a function in the strict sense. So I think it is not clear that this lambda matches the type ft
and therefore can be passed to print_it
(if ft
was defined to be std::function<int(int)>
it would be clear though).
The code example:
#include <iostream>
using ft = int(int);
void print_it (ft f, int a)
{
std::cout << f(a) << std::endl;
}
int main ()
{
auto my_lambda = [] (int a) -> int { return 2 * a; };
print_it (my_lambda, 100);
return (0);
}