Skip Navigation Links | |
Exit Print View | |
![]() |
Oracle Solaris Studio 12.3: C++ User's Guide Oracle Solaris Studio 12.3 Information Library |
3. Using the C++ Compiler Options
6. Creating and Using Templates
6.1.1 Function Template Declaration
6.1.2 Function Template Definition
6.2.1 Class Template Declaration
6.2.2 Class Template Definition
6.2.3 Class Template Member Definitions
6.2.3.1 Function Member Definitions
6.2.3.2 Static Data Member Definitions
6.3.1 Implicit Template Instantiation
6.3.2 Explicit Template Instantiation
6.3.2.1 Explicit Instantiation of Template Functions
6.3.2.2 Explicit Instantiation of Template Classes
6.3.2.3 Explicit Instantiation of Template Class Function Members
6.3.2.4 Explicit Instantiation of Template Class Static Data Members
6.5 Default Template Parameters
6.6.1 Template Specialization Declaration
6.6.2 Template Specialization Definition
6.7.1 Nonlocal Name Resolution and Instantiation
6.7.2 Local Types as Template Arguments
6.7.3 Friend Declarations of Template Functions
6.7.4 Using Qualified Names Within Template Definitions
6.7.6 Referencing Static Variables and Static Functions
6.7.7 Building Multiple Programs Using Templates in the Same Directory
9. Improving Program Performance
10. Building Multithreaded Programs
12. Using the C++ Standard Library
Treating some combinations of template arguments as a special case might provide some performance gains, as in the examples for twice in this section. Alternatively, a template description might fail to work for a set of its possible arguments, as in the examples for sort in this section. Template specialization allows you to define alternative implementations for a given combination of actual template arguments. The template specialization overrides the default instantiation.
You must declare a specialization before any use of that combination of template arguments. The following examples declare specialized implementations of twice and sort.
template <> unsigned twice<unsigned>( unsigned original );
template <> sort<char*>(Array<char*> store);
You can omit the template arguments if the compiler can unambiguously determine them. For example:
template <> unsigned twice(unsigned original);
template <> sort(Array<char*> store);
You must define all template specializations that you declare. The following examples define the functions declared in the preceding section.
template <> unsigned twice<unsigned>(unsigned original) {return original << 1;}
#include <string.h> template <> void sort<char*>(Array<char*> store) {int num_elems = store.GetSize(); for (int i = 0; i < num_elems-1; i++) for (int j = i+1; j < num_elems; j++) if (strcmp(store[j-1], store[j]) > 0) {char *temp = store[j]; store[j] = store[j-1]; store[j-1] = temp;}}
A specialization is used and instantiated just as any other template, except that the definition of a completely specialized template is also an instantiation.
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.