General class specialization for the template

says i have a template class

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

but I wanted to specialize completely. for the template that accepted the parameter?

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

How can I do that?

+3
source share
2 answers

This is called partial specialization and can be encoded as follows:

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 like a normal specialization, but has an additional template argument.

+8
source
template < typename N >
struct Widget< template_thing<N> >
{
  ...
};
+1
source

Source: https://habr.com/ru/post/1785312/


All Articles