Possible Duplicate:
How do I expand a tuple into variadic template function's arguments?
“unpacking” a tuple to call a matching function pointer
In C++11 templates, is there a way to use a tuple as the individual args of a (possibly template) function?
Example:
Let's say I have this function:
void foo(int a, int b)
{
}
And I have the tuple auto bar = std::make_tuple(1, 2)
.
Can I use that to call foo(1, 2)
in a templaty way?
I don't mean simply foo(std::get<0>(bar), std::get<1>(bar))
since I want to do this in a template that doesn't know the number of args.
More complete example:
template<typename Func, typename... Args>
void caller(Func func, Args... args)
{
auto argtuple = std::make_tuple(args...);
do_stuff_with_tuple(argtuple);
func(insert_magic_here(argtuple)); // <-- this is the hard part
}
I should note that I'd prefer to not create one template that works for one arg, another that works for two, etc…
template <typename F, typename Tuple, int N...> call(F f, Tuple const & t) { f(std::get<N>(t)...); }
. Now just fill in the blanks :-) – Phenobarbitalcaller()
templates instead? – SelfevidentN...
, and partially specializing whenN == std::tuple_size<Tuple>::value
, you want to call the original function in the way I suggested. – Phenobarbitalint ...N
, of course.) – Phenobarbital