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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With