C++0x: variadic template puzzle
Hi!
Suppose I stored some parameters in a tuple so I can forward these
parameters _later_ to some function object. I just don't want to
forward the tuple itself but to excract the parameters from the tuple.
However, I have trouble figuring out how to do that. The function
templates I try to define are
template<typename Func, typename... Args>
void tuple_forward(Func && f, tuple<Args...> && args);
template<typename Func, typename... Args>
void tuple_forward(Func && f, tuple<Args...> const& args);
The first takes a tuple rvalue and should extract and forward all
parameters properly to f so it works for move-only types as well. The
second function simply passes the extracted parameters directly to f.
I came up with the following helper class:
template<unsigned... Indices>
struct invoke_helper
{
template<typename Func, typename... Args>
static inline void doit(Func && f, tuple<Args...> && args)
{
f ( forward<Args>(get<Indices>(args))... );
}
template<typename Func, typename... Args>
static inline void doit(Func && f, tuple<Args...> const& args)
{
f ( get<Indices>(args)... );
}
};
The problem is to generate the template parameters "Indices". Here is
an implementation attempt for the first function template:
template<typename Func, typename... Args>
inline void tuple_forward(Func && f, tuple<Args...> && args)
{
typedef invoke_helper<0,1,2,...,(sizeof...(Args)-1)> ih;
ih::doit( forward<Func>(f) , move(args) );
// Note: "args" is always an rvalue reference. There is
// no reference collapsing involved.
}
I havn't yet been able to come up with a solution for the "ih"-
typedef. Obviously the line is ill-formed. But it shows the intention.
Is there any solution to this problem? It yes, what does it look like?
Cheers!
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]