I have a template function with varargs template arguments, like this
template
void ascendingPrint(
Here is a recursive implementation of a specialized revert<>:
// forward decl
template
struct revert;
// recursion anchor
template<>
struct revert<>
{
template
static void apply(Un const&... un)
{
ascendingPrint(un...);
}
};
// recursion
template
struct revert
{
template
static void apply(T const& t, Tn const&... tn, Un const&... un)
{
// bubble 1st parameter backwards
revert::apply(tn..., t, un...);
}
};
// using recursive function
template
void descendingPrint(A const& a, An const&... an)
{
revert::apply(an..., a);
}
It works with gcc-4.6/7/8 and clang and is probably standard compliant -- the only difficult part being the call of revert.
It has drawbacks though (as recursion often has), that it generates a lot of template-instantiations of the target function (code bloat) and does not use perfect forwarding, which may be an issue (but maybe could be improved to use it).