Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a lambda on the heap in one step? [duplicate]

Tags:

c++

c++17

We can create a lambda like this:

auto x = [](){};

I can create a copy of this on the heap like this:

auto y = new decltype(x)(x);

The question is, is it possible to do this in one step? Creating a lambda on the heap without extra steps?

like image 795
geza Avatar asked Aug 14 '18 10:08

geza


People also ask

How are lambdas implemented in C++?

The context of a lambda is the set of objects that are in scope when the lambda is called. The context objects may be captured then used as part of the lambda's processing. Capturing an object by name makes a lambda-local copy of the object. Capturing an object by reference allows the lambda to manipulate its context.

Are lambdas copyable?

Copying a lambda will copy its state.


1 Answers

You can use auto in a new-expression:

new auto ([](){});
like image 70
Oliv Avatar answered Nov 12 '22 18:11

Oliv