Definition and declaration of explicit specializations

The definition of an explicitly specialized class is unrelated to the definition of the primary template. You do not have to define the primary template in order to define the specialization (nor do you have to define the specialization in order to define the primary template). For example, the compiler will allow the following:

template<class T> class A;
template<> class A<int>;

template<> class A<int> { /* ... */ };

The primary template is not defined, but the explicit specialization is.

You can use the name of an explicit specialization that has been declared but not defined the same way as an incompletely defined class. The following example demonstrates this:

template<class T> class X { };
template<>  class X<char>;
X<char>* p;
X<int> i;
// X<char> j;

The compiler does not allow the declaration X<char> j because the explicit specialization of X<char> is not defined.



[ Top of Page | Previous Page | Next Page | Contents | Index ]