I have a function f1()
template <typename... Args>
void f1(Args... args)
{
// the implementation is just an example, I don't really need a complicated
// way to sum numbers
boost::fusion::vector<Args...> v(args...);
std::cout << boost::fusion::accumulate(v, 0, [](auto i1, auto i2) { return i1 + i2; }) << std::endl;
}
I want to call it from a function f2()
, but with a different last argument. Is there a simple approach? I tried a naive one
template <typename... Args>
struct CallHelper;
template <>
struct CallHelper<>
{
template <typename... Args>
static void Apply(Args... args) { f1(args...); }
};
template <typename A0>
struct CallHelper<A0>
{
template <typename... Args>
static void Apply(Args ...args, A0 a0)
{
// substitute 10 to the last argument
CallHelper<>::Apply(args..., 10);
}
};
template <typename Head, typename ...TailArgs>
struct CallHelper<Head, TailArgs...>
{
template <typename... Args>
static void Apply(Args... args, Head head, TailArgs ...tailArgs)
{
CallHelper<TailArgs...>::Apply(args..., head, tailArgs...);
}
};
template <typename... Args>
void f2(Args... args)
{
CallHelper<Args...>::Apply(args...);
}
Of course it doesn't work, because Head head
is not the first argument. Maybe there is a way to make Head head
a parameter pack as well? Or there is something else I can do?