D
D
Daniil Demidko2016-03-22 11:28:23
C++ / C#
Daniil Demidko, 2016-03-22 11:28:23

Is it possible to return lambdas?

auto GetLambda() {
    return []( const int &x ) { return x*x ; } ;
}

Doing this is normal? And what is returned in this case, a pointer to the function or the function itself?
If a pointer is returned, where is the function itself stored, on the heap or on the stack?
And another question on the same topic:
auto lambda = [] ( const int &x ) { return x*x ; } ;

This is an abbreviated notation of this construction:
int ( * const fn ) (const int &) = [] ( const int &x ) { return x*x ; } ;

Or something different?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-03-22
@Daniro_San

Doing this is normal?
Quite
And what is returned in this case, a pointer to the function or the function itself?
functional object. Lambda is not a function , it can be cast to a function pointer in some cases.
If a pointer is returned, where is the function itself stored, on the heap or on the stack?
The "function" of the lambda is stored in the same place as the rest of the code. On the fly, nothing is collected and compiled. A lambda is syntactic sugar for declaring a class with an overloaded function call operator.
And one more question on the same topic:
This is an abbreviated notation like this:
Or something else?
lambda has a unique type. It's not a function (but can be cast to in this case).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question