dimanche 15 septembre 2019

How Lambda's members are initalized?

It is said that members of lambda are initialized when lambda is defined not when an object of that lambda has been created. To understand this more I've made a function foo that prints a simple message(check of being called later) and returns an integer value ( arbitrary value. here 1024) used to initialize a member of lambda.

Inside the lambda body it prints the value of its captured object.

int foo() {
    std::cout << "foo()" << std::endl;
    return 1024;
}

int main() {

    int x = 0;
    [x = foo()]()mutable{ x = foo();  cout << "in un-named lambda x: " << x << endl; };
}

The output:

foo()

Why I get only foo() but not:

foo()
foo()
in un-named lambda x: 1024

  • Does this mean [x = foo()] is an initialization and in {x = foo()} is an assignment?

Aucun commentaire:

Enregistrer un commentaire