I can not understand what needs to be done so that you can operate with pointers to a lambda expression with variable capture, as well as without capturing variables? And in the third example, why functions f and f3 cannot be called, but function f2 is called?

// ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€(ΡƒΡΠΏΠ΅ΡˆΠ½Ρ‹ΠΉ) auto fo = [](void*) ->unsigned {std::cout << 5 << std::endl; return 0; }; unsigned(*fo2) (void*); decltype (auto) fo3 = fo; fo2 = fo; // компилируСтся. Π’ Ρ‡Π΅ΠΌ ΠΎΡ‚Π»ΠΈΡ‡ΠΈΠ΅ ΠΎΡ‚ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°? fo2(nullptr);// компилируСтся fo(nullptr); // компилируСтся fo3(nullptr);// компилируСтся // Π²Ρ‚ΠΎΡ€ΠΎΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€ Ρ‚Π°ΠΊΠΎΠΉ ΠΆΠ΅ ΠΊΠ°ΠΊ ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ, Π½ΠΎ с Π·Π°Ρ…Π²Π°Ρ‚ΠΎΠΌ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… лямбда Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ auto foo = [&](void*) ->unsigned {std::cout << 5 << std::endl; return 0; }; unsigned(*foo2) (void*); decltype (auto) foo3 = foo; foo2 = foo;// НЕ компилируСтся. ΠŸΠΎΡ‡Π΅ΠΌΡƒ? foo(nullptr); // компилируСтся foo3(nullptr);// компилируСтся // Ρ‚Ρ€Π΅Ρ‚ΠΈΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€ auto *f = &[](void*) ->unsigned {std::cout << 5 << std::endl; return 0; }; unsigned(*f2) (void*); decltype (auto) f3 = f; f2 = *f;// компилируСтся f2(nullptr);// компилируСтся // ΠΊΠ°ΠΊ Π·Π°ΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ f ΠΈ f3 ? f(nullptr);// НЕ компилируСтся *(f(nullptr));// НЕ компилируСтся f3(nullptr);// НЕ компилируСтся *(f3(nullptr));// НЕ компилируСтся 

    1 answer 1

    Capturing variables is an optional function argument. There is no possibility for the compiler to convert the lambda type to the usual one.

     void f(){ int x = 5 ; auto foo = [&](void) ->int {++ x ; return x; }; foo(); } 

    The lambda function in the code is implemented with the argument ( $ g++ -std=c++11 -S -Wall -Wpedantic ):

     [](int**ppx) ->int {++ (**ppx) ; return **ppx; }; 

    The compiler decides how to create a lambda implementation. And to transfer this type of function has no opportunity.