Search code examples
c++templatesvariadic-templates

How to specialize variadic template class for one type?


I am trying to specialize variadic template class for specific type.

I am trying to achieve this:

template<typename... Ts>
class myclass
{
   ///...
};

template<>
class myclass<int... N>
{
   ///...
};

And I get this errord:

error C2760: syntax error: unexpected token 'int', expected 'expression'
error C2187: syntax error: '...' was unexpected here
error C2065: 'N': undeclared identifier
error C2913: explicit specialization; 'ex::vec' is not a specialization of a class templa

Can anyone please hint what am I doing wrong?


Solution

  • Your myclass is declared for a variadic list of template parameter

    template<typename... Ts>
    class myclass
    

    You can specialize if for specific types, not for specific values, as in your question (corrected for syntax, but remain wrong for the types/values problem)

    template <int N>
    class myclass<N...>
    

    Different if you have the values as template parameter of a class, something as

    template <typename ... Ts>
    class myclass
     { };
    
    template <template <int...> class C, int ... Is>
    class myclass<C<Is...>>
     { };
    
    // ...
    
    template <int ...>
    class foo 
     { };
    
    // ...
    
    myclass<foo<0, 1, 2>>  m0; // uses specialization