Search code examples
c++templatesvariadic-templatestemplate-argument-deduction

C++ argument pack recursion with two arguments


I have a template class that uses two types:

template<typename T1,typename T2> class Foo { ... };

I need to write a function that takes any number of Foo variables:

template <typename T1, typename T2, typename... Others> size_t getSize(Foo<T1,T2> *f, Foo<Others>*... o) { ... };

If I implement the class Foo with only one template parameter, it works well. But with two (or more) parameters, the compiler complains that Foo<Others> requires two args.

Is is possible to achieve argument pack forwarding when the class Foo has multiple template parameters ?


Solution

  • What about

    template <typename ... Ts1, typename ... Ts2>
    std::size_t get_size (Foo<Ts1, Ts2> * ... fs)
     { /* ... */ }
    

    ?

    Or, maybe,

    template <typename T1, typename T2, typename ... Us1, typename ... Us2>
    std::size_t get_size (Foo<T1, T2> * f, Foo<Us1, Us2> * ... fs)
     { /* ... */ }
    

    if you want a first Foo managed differently.