vendredi 19 avril 2019

How to unroll a parameter pack from right to left

I am trying to do a parameter pack unrolling by dispatching to a class recursively. I'd like to do that from right to left since some of the operations do pre-pending.

template <typename.... T>
class Foo;

template <typename T>
class Foo<T> {/* base case implementation*/};

template <typename T, typename R, typename... Rs>
class Foo<T, Rs..., R> {
   private:
     Foo<T, Rs...> foo_;
}

Unfortunately, the above gets me:

class template partial specialization contains template parameters that cannot be deduced; this partial specialization will never be used

This seems odd to me, I would assume that even though the arguments has switched order, Foo<T, Rs..., R> should still match the template specialization.

I've looked at some similar questions:

Specifically, C++ template partial specialization: Why cant I match the last type in variadic-template?

However, the highest voted (non-accepted) answer doesn't make sense to me. Sure I understand that the template parameter pack declaration must be the last in the declaration, but I do that for the template specialization - i'm not sure why the compiler cannot map Foo<T, Rs..., R> to the initial template declaration Foo<T...> and enforces the parameter pack declaration order there.

Other answers on that thread offer how to extract the last value, but that still doesn't allow you to do recursive parameter pack unrolling, which is sort of the whole point here. Is it just impossible to unroll a parameter pack from right to left?

Aucun commentaire:

Enregistrer un commentaire