template class member function only specialization

风格不统一 提交于 2019-11-27 19:41:14

I think it is referring to the following case:

template <typename T>
struct base {
   void foo() { std::cout << "generic" << std::endl; }
   void bar() { std::cout << "bar" << std::endl; }
};
template <>
void base<int>::foo() // specialize only one member
{ 
   std::cout << "int" << std::endl; 
}
int main() {
   base<int> i;
   i.foo();         // int
   i.bar();         // bar
}

Once that is done, you cannot specialize the full template to be any other thing, so

template <>
struct base<int> {};  // error

I think what is meant is that you can either:

  • specialize the whole class and all members (data and functions, static or not, virtual or not) have to be declared and defined even if they are the same as for the non specialized version,

  • specialize some function members, but then you can't specialize the whole class (i.e. all members are declared in the same way as for the non specialized case, you just provide the implementation for some function members).

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