What exactly happens with a lambda expression

May 29, 2013 at 7:11am
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
*/
May 29, 2013 at 8:20am
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
May 29, 2013 at 12:51pm
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)


May 29, 2013 at 8:22pm
Oh, okay. But if we wanted to, we could dereference the lambda expression?
May 30, 2013 at 2:20am
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.
May 30, 2013 at 2:27am
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.