According to cppreference.com, C++11 lambda literal syntax is only legal to use in direct initialization. There doesn't seem to be a way to use the lambda syntax directly with the new
operator.
I need to store a lambda function in the heap so that I can call it at some later point from a different thread. It's easy enough to make a copy of the lambda, but is there a simple way to allocate the lambda directly in the heap (dynamic storage duration) without first allocating it on the stack (automatic storage duration) and making a copy?
Here's a simple example:
#include <cstdio>
#include <cassert>
struct MyObj {
int value;
int copies;
int moves;
MyObj(int v): value(v), copies(0), moves(0) {
printf("Created object with value %d.
", value);
}
MyObj(const MyObj &other): value(other.value),
copies(other.copies+1), moves(other.moves) { }
MyObj(const MyObj &&other): value(other.value),
copies(other.copies), moves(other.moves+1) { }
};
int main (int argc, char **argv) {
MyObj o { 5 };
// Create lambda on stack (automatic storage duration)
auto f = [o] {
printf("Object value is %d
", o.value);
printf("%d copies, %d moves...
", o.copies, o.moves);
};
// Copy lambda to heap (dynamic storage duration)
decltype(f) *g = new decltype(f)(f);
// Call the copy
(*g)();
return 0;
}
The above program makes 2 copies of o
(one in the capture, and another when the lambda is copied into the heap). Ideally, there would only be one copy or move, which would happen when the heap-allocated lambda captures a copy of o
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…