total class specialization for a template

二次信任 提交于 2020-01-04 02:57:08

问题


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> >
{
  ...
};


来源:https://stackoverflow.com/questions/4686445/total-class-specialization-for-a-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!