Home > Back-end >  How to do a template specialization with multiple arguments (when the type of one of them is known)
How to do a template specialization with multiple arguments (when the type of one of them is known)

Time:10-03

Here's the template for a class

 template <typename T, unsigned int size>
 class Foo { 
       T myarray[size];
       //other code (doesn't matter for the example)
 }

and I want to do a template specialization for it. For example, I have defined class Bar. So I want to make a specialization for class Bar, but I want to keep unsigned int size. How do I do that?

template <>
class Foo<Bar, unsigned int> {   //throws error "type name not allowed" for unsigned int
      Bar myarray[size];
      //other code
}

   

CodePudding user response:

This is called partial template specialization:

template<unsigned int size>
class Foo<Bar, size> {

    Bar myarray[size];
};
  • Related