The lambda is not a pointer, but can be converted into a pointer-to-function.
Functions are not "stored" like values in C++. In practice, they exist in a code segment of the executable, and are loaded into write-protected execute-bit-set pages by the executable/dll loader.
The stateless lambda's code is no different. The conversion just returns a pointer to a function with the same effect as the body of the lambda, no more, no less.
Remember this only works with stateless lambdas. There is no non-static data to be stored.
Now, the +
thing is a bit of a trick, in that when you apply unary operator+
to an object, conversion is attempted, and one uniqie type (conversion of the function object to function pointer) is found.
I guess concrete code may help.
struct fake_lambda {
static void action(){ std::cout<<"hello?\n"; }
void operator()()const{action();}
using ptr=void(*)();
operator ptr()const{return &fake_lambda::action;}
};
Now auto f=+fake_lambda{};
is a pointer to a function that prints "hello?\n"
.
This is basically the same as auto f=+[](){std::cout<<"hello\n";};
, but more verbose.