Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am reading the Complete Guide on Templates and it says the following:

Where it is talking about class template specialization.

Although it is possible to specialize a single member function of a class template, once you have done so, you can no longer specialize the whole class template instance that the specialized member belongs to.

I'm actually wondering how this is true, cause you can specialize without any member functions at all. Is it saying that you cannot have a specialization with only one member function and then another with all member functions?

Can someone please clarify?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
318 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...