Re: Variadic templates
On Nov 18, 3:42 am, Boris Rasin <rasin.bo...@gmail.com> wrote:
On Nov 17, 10:45 pm, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
All you need is an helper function like this:
template <class T, class... Args>
/* whatever */ invoke(T f, tuple<Args...>&& args);
which calls f with the supplied arguments. It should not be too
difficult to make it a perfect forwarder.
Ganesh
Seeing that it should not be too difficult, would you mind actually
showing how to call a function with any number of parameters given
tuple<> object as parameter container (as in your sample)? No need to
make it a perfect forwarding, just anything that would actually work.
Thank you very much.
Semi-tested code that I happened to have lying around:
template<class... T> struct emplacer_t;
template<class H, class... T> struct emplacer_t<H, T...>
{
emplacer_t(H&& head, T&&... tail)
: head(head), tail(tail...)
{}
H&& head;
emplacer_t<T...> tail;
};
template<> struct emplacer_t<> {};
template<class... T> emplacer_t<T...> emplace(T&&... t)
{
return emplacer_t<T...>(std::forward<T>(t)...);
}
template<class... Unpacked, Callable<auto, Unpacked&&...> F>
F::result_type
inline apply_args(emplacer_t<> e, F f, Unpacked&&... u)
{
return f(std::forward<Unpacked>(u)...);
}
template<class RestHead, class... RestTail, class... Unpacked,
Callable<auto, Unpacked&&..., RestHead&&, RestTail&&...> F>
F::result_type
inline apply_args(emplacer_t<RestHead, RestTail...> e, F f,
Unpacked&&... u)
{
return apply_args(emplacer_t<RestTail...>(e.tail), f,
std::forward<Unpacked>(u)..., std::forward<RestHead>(e.head));
}
To compile it under concept-gcc I had to remove std::Callable (which
means removing result_type) and std::forward.
Here the argument list is in a structure called emplace_t -- to unpack
std::tuple is slightly more difficult because there's no easy way to
get the tail, but it can be done.
Yechezkel Mett
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]