Variadic Template Function Argument Expansion from Tuples
Consider a templated function with variadic template parameters:
template<typename Tret, typename... T> Tret func(const T&... t);
How can you call func() using a tuple of values as the arguments?
Modern C Solution (C 17 and Later)
In C 17, the std::apply() function provides an elegant solution:
std::apply(the_function, the_tuple);
Clang 3.9 Workaround
If you're using Clang 3.9, you can use the std::experimental::apply function instead.
Handling Templated Functions
If the_function is templated, you can use the following workaround:
#include <tuple> template <typename T, typename U> void my_func(T &&t, U &&u) {} int main() { std::tuple<int, float&> my_tuple; std::apply([](auto &&... args) { my_func(args...); }, my_tuple); return 0; }
Generalized Overload Set Invocation
For a more versatile solution that can handle overload sets and function templates, refer to the comprehensive explanation at https://blog.tartanllama.xyz/passing-overload-sets/.
The above is the detailed content of How Can I Pass a Tuple of Values as Arguments to a Variadic Template Function in C ?. For more information, please follow other related articles on the PHP Chinese website!