Unpacking a parameter package in C ++

I have two functions f and g . f calculates this return value asynchronously and returns the future. Now, based on the multiple return values โ€‹โ€‹of f , I want to call g , but I want to make sure that the calculation of the values โ€‹โ€‹of f happens in parallel.

Consider the following code:

 template <typename T> std::future<T> f(T& t); template <typename... T> void g(T&&... t) template <typename... T> void call_wrapper(T&&... t) { auto f1 = f(t1); // How do I set the values of f1... fn auto f2 = f(t2); ... g(f1.get(), f2.get()....); // How do I call g } 

How can I unpack types from the variational template T the call_wrapper function?

+5
source share
2 answers

[Edit2: I think I misunderstood the question, I forgot that subzero wanted to return std::future and just thought that the only problem was the syntax of the parameter package. Hopefully using a helper function like in my first edit should work at least]

You can simply do:

 template <typename... T> void call_wrapper(T&&... t) { g(f(std::forward<T>(t)).get()...); } 

If I do not understand what you want to do. C>

Edit1: if you want to do something else, you can split the function into two calls, for example:

 template<typename... T> void helper(T&&... t) { // ... g(std::forward<T>(t).get()...); } template <typename... T> void call_wrapper(T&&... t) { helper(f(std::forward<T>(t))...); } 
+5
source

Here's a quick solution storing std::future in std::tuple :

 template <class T, std::size_t... Idx> void callG(T &tuple, std::index_sequence<Idx...>) { g(std::get<Idx>(tuple).get()...); } template <typename... T> void call_wrapper(T&&... t) { auto results = std::make_tuple(f(std::forward<T>(t))...); callG(results, std::index_sequence_for<T...>{}); } 

Live on coliru

+3
source

All Articles