How do I expand a tuple into variadic template function's arguments?
arguments, c++, c++11, tuples
Solution
In C++17 you can do this:
std::apply(the_function, the_tuple);
This already works in Clang++ 3.9, using std::experimental::apply.
Responding to the comment saying that this won't work if `the_function` is templated, the following is a work-around:
#include <tuple>
template <typename T, typename U> void my_func(T &&t, U &&u) {}
int main(int argc, char *argv[argc]) {
std::tuple<int, float> my_tuple;
std::apply([](auto &&... args) { my_func(args...); }, my_tuple);
return 0;
}
This work around is a simplified solution to the general problem of passing overload sets and function template where a function would be expected. The general solution (one that is taking care of perfect-forwarding, constexpr-ness, and noexcept-ness) is presented here: https://blog.tartanllama.xyz/passing-overload-sets/.
Problem
Consider the case of a templated function with variadic template arguments: ``` template<typename Tret, typename... T> Tret func(const T&... t); ``` Now, I have a tuple `t` of values. How do I call `func()` using the tuple values as arguments? I've read about the `bind()` function object, with `call()` function, and also the `apply()` function in different some now-obsolete documents. The GNU GCC 4.4 implementation seems to have a `call()` function in the `bind()` class, but there is very little documentation on the subject. Some people suggest hand-written recursive hacks, but the true value of variadic template arguments is to be able to use them in cases like above. Does anyone have a solution to is, or hint on where to read about it?