Creating a future from intermediate futures?
c++, c++11
Solution
It occurred to me that I can use `std::async` with the deferred launch policy to compose the final object:
std::future<Item> get_item()
{
// start async creation of component
// (using shared_future to make it copyable)
std::shared_future<Component> component = get_component();
// deferred launch policy can be used for construction of the final object
return std::async(std::launch::deferred, [=]() {
return Item(component.get());
});
}
Problem
In the following sample code I want to create an `Item` object from a `Component`: ``` struct Component { }; struct Item { explicit Item(Component component) : comp(component) {} Component comp; }; struct Factory { static std::future<Item> get_item() { std::future<Component> component = get_component(); // how to get a std::future<Item> ? } std::future<Component> get_component(); }; ``` How do I go from `std::future<Component>` to `std::future<Item>`? Update: removed my first idea (which was thread-based) from the question and posted an answer.