dimanche 17 décembre 2017

How do I write a variadic template function in C++ where the parameter pack is not the last parameter?

I'm trying to use a variadic template function where the parameter pack is not the last parameter in the list. Note that there are two recursive calls--one dropping a parameter in front of the pack, the other call dropping a parameter after the pack.

  • My compiler appears to be: Apple LLVM version 8.1.0 (clang-802.0.42)
  • All the int's below will be a new T template parameter if I can get this working.

There's no point in using ... if the call site for Blender can't be clean. I could just expand several overloads of Blender myself in that case. I'd really rather not resort to this. I hope I'm just missing something.

int Blender( double t, int i)
{
    return i;
}

template <typename ...Args>
  int Blender( double t, int first, Args... more, int last)
{
    return (1-t)*Blender(t, first, more...) + t*Blender(t, more..., last);
}

static void tryit()
{
    Blender(.5, 23, 42, 89); //doesn't compile
}

Aucun commentaire:

Enregistrer un commentaire