I want to make a state machine which works processes the submitted signals in its own thread. I use Visual Studio 2015, so C++11 and partially C++14 is supported. Signals are stored in containers. Each signal is represented as an std::function. I would like to wait from the client until the state machine processes the signal that was submitted, so it is a kind of synchronous signal.
My problem is: I cannot capture an std::promise into a 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 the promise by reference
- I want to avoid capturing a * pointer or a shared_ptr to the promise
It would be nice to embed the promise into the class somehow that the lambda generates. This means that the lambda is not copyable any more, only moveable. Is it possible at all?
Aucun commentaire:
Enregistrer un commentaire