I want to create a state machine that processes the transmitted signals in its stream. I use Visual Studio 2015, so C ++ 11 and partially C ++ 14 are supported. Signals are stored in containers. Each signal is represented as a function of std ::. I would like to wait from the client until the state machine processes the signal that was sent, so this is a kind of synchronous signal.
My problem: I cannot commit std :: prom to lambda and add it to the container.
#include <functional> #include <future> #include <list> std::list<std::function<int()>> callbacks; void addToCallbacks(std::function<int()>&& callback) { callbacks.push_back(std::move(callback)); } int main() { std::promise<int> prom; auto fut = prom.get_future(); // I have made the lambda mutable, so that the promise is not const, so that I can call the set_value auto callback = [proms{ std::move(prom) }]() mutable { proms.set_value(5); return 5; }; // This does not compile addToCallbacks(std::move(callback)); // This does not compile either, however this lambda is a temporal value (lvalue) addToCallbacks([proms{ std::move(prom) }]() mutable { proms.set_value(5); return 5; }); return 0; }
What are the solutions if
- I want to avoid capturing a promise with a link
- I want to avoid capturing a * or shared_ptr pointer to promise
It would be nice to put a promise in the class anyway, like a lambda is generated. This means that the lambda is no longer copied, but only movable. Is it possible at all?
c ++ promise lambda c ++ 11 c ++ 14
user2281723
source share