Oracle® Solaris Studio 12.4: C++ User's Guide

Exit Print View

Updated: March 2015
 
 

6.6.4 Partial Specialization

In the previous examples, the templates are fully specialized. That is, they define an implementation for specific template arguments. A template can also be partially specialized, meaning that only some of the template parameters are specified, or that one or more parameters are limited to certain categories of type. The resulting partial specialization is itself still a template. For example, the following code sample shows a primary template and a full specialization of that template.

template<class T, class U> class A {...}; //primary template
template<> class A<int, double> {...};    //specialization

The following code shows examples of partial specialization of the primary template.

template<class U> class A<int> {...};          // Example 1
template<class T, class U> class A<T*> {...};  // Example 2
template<class T> class A<T**, char> {...};    // Example 3
  • Example 1 provides a special template definition for cases when the first template parameter is type int.

  • Example 2 provides a special template definition for cases when the first template parameter is any pointer type.

  • Example 3 provides a special template definition for cases when the first template parameter is pointer-to-pointer of any type, and the second template parameter is type char.