Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to delete lambdas?

Tags:

I am storing pointers to lambdas in dynamically allocated objects:

struct Function {     SomeType*(*func)(int);     Function(SomeType*(*new_func)(int)):         func(new_func) {} }  Function* myf = new Function(     [](int x){ return doSomething(x); } );  delete myf; 

Do I have to write something special in the destructor of this class?

like image 325
user6245072 Avatar asked Oct 30 '16 16:10

user6245072


People also ask

What is Lambda delete?

Lambda Delete / 02 OFF The second lambda sensor is used for monitoring the efficiency of the catalytic converter, by disabling the second lambda sensor it will allow a decat pipe to be used on a vehicle without triggering an engine management light or associated fault codes.

What is the point of lambdas?

Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one.


1 Answers

No, you do not need to do anything special. In this case (you're converting the lambda to a function pointer) this is no different to telling you that you don't need to delete doSomething either.

More generally, lambdas are unnamed types with deleted default constructors. This means you can only explicitly create one with new expression by copy/move constructing it - and only then you'd have to call delete.

N4140 §5.1.2 [expr.prim.lambda] /20

The closure type associated with a lambda-expression has a deleted default constructor and a deleted copy assignment operator.

like image 58
krzaq Avatar answered Nov 14 '22 04:11

krzaq