Tony The Lion
Tony The Lion

Reputation: 63190

template class member function only specialization

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?

Upvotes: 39

Views: 12504

Answers (3)

Hari
Hari

Reputation: 1797

Explicit and implicit instantiation and situations like the following could be kept in mind.

When a class is explicitly instantiated (template class Class_Template<Explicit_Type>;), all members of the generic class will be present in the instantiated class. However, suppose a class specialisation is described by mentioning a class body. Only members (whether members that are already present in the generic class or new members) that are explicitly defined in the specialized class will be present in the instantiated class.

Suppose member functions (of the generic class) are defined via a particular scoped class specialization, then a specialized class is implicitly instantiated which will have these functions and also all other members present in the generic class, in a suitably specialized manner.

In the second case, since a specialized class has been implicitly instantiated, we cannot later also define a class with the same specialization, either via explicit instantiation or by mentioning a class body. The need to do the latter may arise in order to add a member that is specific to that specialization. That is not possible in this case, since the compiler has already instantiated a specialized class.

Irrespective of these ways in which a class specialization could be instantiated, the user is always free to instantiate other specializations based on other template arguments.

Upvotes: 0

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

Upvotes: 43

AProgrammer
AProgrammer

Reputation: 52274

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).

Upvotes: 4

Related Questions