简体   繁体   中英

total class specialization for a template

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM