Semantically, are there real lambdas functions in C ++? How can I point to a lambda expression to prove it?

Given the following typedef function pointer:

typedef void(*vfp)();

We can, of course, point to the void function as follows:

string s = "Gordon Freeman: Lambdas have no half-lives.";

void f() {
    cout << s;
}

int main(int argc, char **argv)
{
    vfp p;
    p = f;
    p();
}

But how can I point to a lambda expression?

typedef void(*vfp)();

int main(int argc, char **argv)
{
    string s = "Gordon Freeman: Lambdas have no half-lives.";
    vfp p;
    p = [&s]()->void{cout<<s;}; // Error, lambdas do not return "function objects" as in Python.
    p();
}

Error:

cannot convert 'main (int, char **) :: __ lambda0' to 'vfp {aka void (*) ()}' in the assignment.

So, if I cannot point to an anonymous lambda function in the same way, I can point to a function, they are not functions (And they should be. If it is not, why are they not?)

+4
source share

All Articles