What exactly happens with a lambda expression

Every tutorial I've read about lambda functions basically state the following from http://www.cplusplus.com/faq/intro/:
A simple lambda is supposed to decompose to a function pointer.

and that lambda functions can be assigned to function pointer variables.

My question is: if they really do decompose into function pointers, why is this syntax used to immediately call it?
1
2
3
4
5
int x = [](){return 3;}(); //Pathetic but you get the point
//Why not do this:
/*
int x = *([](){return 3;})(); //Dereferencing resultant function pointer
*/
If you are speaking about this statement

int x = [](){return 3;}();

then here the function call operator is called.

From the C++ Standard:

The lambda-expression’s compound-statement yields the function-body (8.4) of the function call operator
if they really do decompose into function pointers, why is this syntax used to immediately call it


Besides that lambdas have their own function call operators, function pointers don't need to be dereferenced to be called: the function-call operator is defined for pointers to functions in C++

(fun useless trivia: in C, function call operator is ONLY defined for pointers to functions)


Oh, okay. But if we wanted to, we could dereference the lambda expression?
yes of course (as long as the lambda is captureless), but mind the operator precedence. Your example is trying to dereference the result of the function call.
Ah, okay thanks, and I will definitely remember next time to enclose my dereference in another pair of brackets.
Topic archived. No new replies allowed.