In addition to operator()
, a the class defined by a lambda can (under the right circumstances) provide a conversion to a pointer to function. The circumstance (or at least the primary one) is that the lambda can’t capture anything.
If you add a capture:
auto f1 = get(
[]() { std::cout << "lambda1::operator()()\n"; },
[i](int) { std::cout << "lambda2::operator()(int)\n"; }
);
f1();
f1(2);
…the conversion to pointer to function
is no longer provided, so trying to compile the code above gives the error you probably expected all along:
trash9.cpp: In function 'int main(int, char**)':
trash9.cpp:49:9: error: no match for call to '(Overload<main(int, char**)::<lambda()>, main(int, char**)::<lambda(int)> >) (int)'
trash9.cpp:14:8: note: candidate is:
trash9.cpp:45:23: note: main(int, char**)::<lambda()>
trash9.cpp:45:23: note: candidate expects 0 arguments, 1 provided