Search code examples
c++variadic-templates

template of variadic template: parameter pack expects a type template


template <typename T, template <typename T, typename... Targs> class C>
void print_(const C<T,Targs>& arg, std::stringstream& ss)
{
    std::for_each(arg.begin(), arg.end(), [&ss](const T& var) { ss << var; });
}

int main()
{
   vector<int> vec{1,2,3};
   std::stringstream ss;
   print_(vec,ss);
}

main.cpp(8): error C2065: 'Targs': undeclared identifier
main.cpp(8): error C3544: 'Targs': parameter pack expects a type template


Solution

  • Names that are part of a template parameters list introduced by a template template parameter are valid only within that list.
    In other terms, something like this is legal:

    template<template<typename T, T> class U>
    struct S {};
    

    And you can use it as it follows:

    template<typename T, T>
    struct A {};
    
    template<template<typename T, T> class U>
    struct B {};
    
    int main() {
        B<A> b;
    }
    

    This isn't valid code instead:

    template <typename T, template <typename T, typename... Targs> class C>
    void print_(const C<T,Targs> &, std::stringstream &);
    

    Targs is no longer a valid name after the first > symbol (the one that follows immediately Targs itself).
    The compiler keeps saying it to you quite clearly: undeclared identifier.


    That being said, in your question there isn't an actual question, so I don't know what you expect from an answer but to tell you why that's an error.