total class specialization for a template

2019-07-02 21:47发布

问题:

lets say i have a templated class

template <typename T>
struct Widget
{
   //generalized implementation
}

but i wanted to totally specialize.. for a template that accepted a parameter?

template <>
struct Widget< TemplateThatAcceptsParameter<N> >
{
       //implementation for Widget for TemplateThatAcceptsParameterN 
       //which takes parameter N
}

How does one go about doing this?

回答1:

This is called a partial specialization and can be coded like this:

template <typename T>
struct Widget
{
   //generalized implementation
};

template <typename N>
struct Widget< TemplateThatAcceptsParameter<N> >
{
   //implementation for Widget for TemplateThatAcceptsParameterN 
   //which takes parameter N
};

It works just like a regular specialization, but has an extra template argument.



回答2:

template < typename N >
struct Widget< template_thing<N> >
{
  ...
};