JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle Solaris Studio 12.3: C++ User's Guide     Oracle Solaris Studio 12.3 Information Library
search filter icon
search icon

Document Information


Part I C++ Compiler

1.  The C++ Compiler

2.  Using the C++ Compiler

3.  Using the C++ Compiler Options

Part II Writing C++ Programs

4.  Language Extensions

5.  Program Organization

6.  Creating and Using Templates

6.1 Function Templates

6.1.1 Function Template Declaration

6.1.2 Function Template Definition

6.1.3 Function Template Use

6.2 Class Templates

6.2.1 Class Template Declaration

6.2.2 Class Template Definition

6.2.3 Class Template Member Definitions Function Member Definitions Static Data Member Definitions

6.2.4 Class Template Use

6.3 Template Instantiation

6.3.1 Implicit Template Instantiation

6.3.2 Explicit Template Instantiation Explicit Instantiation of Template Functions Explicit Instantiation of Template Classes Explicit Instantiation of Template Class Function Members Explicit Instantiation of Template Class Static Data Members

6.4 Template Composition

6.5 Default Template Parameters

6.6 Template Specialization

6.6.1 Template Specialization Declaration

6.6.2 Template Specialization Definition

6.6.3 Template Specialization Use and Instantiation

6.6.4 Partial Specialization

6.7 Template Problem Areas

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.5 Nesting Template Names

6.7.6 Referencing Static Variables and Static Functions

6.7.7 Building Multiple Programs Using Templates in the Same Directory

7.  Compiling Templates

8.  Exception Handling

9.  Improving Program Performance

10.  Building Multithreaded Programs

Part III Libraries

11.  Using Libraries

12.  Using the C++ Standard Library

13.  Using the Classic iostream Library

14.  Building Libraries

Part IV Appendixes

A.  C++ Compiler Options

B.  Pragmas



6.2 Class Templates

A class template describes a set of related classes or data types that differ only by types, by integral values, by pointers or references to variables with global linkage, or by a combination thereof. Class templates are particularly useful in describing generic but type-safe data structures.

6.2.1 Class Template Declaration

A class template declaration provides only the name of the class and its template arguments. Such a declaration is an incomplete class template.

The following example is a template declaration for a class named Array that takes any type as an argument.

template <class Elem> class Array;

This template is for a class named String that takes an unsigned int as an argument.

template <unsigned Size> class String;

6.2.2 Class Template Definition

A class template definition must declare the class data and function members, as in the following examples.

template <class Elem> class Array {
        Elem* data;
        int size;
        Array( int sz );
        int GetSize();
        Elem& operator[]( int idx );
template <unsigned Size> class String {
        char data[Size];
        static int overflows;
        String( char *initial );
        int length();

Unlike function templates, class templates can have both type parameters (such as class Elem) and expression parameters (such as unsigned Size). An expression parameter can be:

6.2.3 Class Template Member Definitions

The full definition of a class template requires definitions for its function members and static data members. Dynamic (nonstatic) data members are sufficiently defined by the class template declaration. Function Member Definitions

The definition of a template function member consists of the template parameter specification followed by a function definition. The function identifier is qualified by the class template’s class name and the template arguments. The following example shows definitions of two function members of the Array class template, which has a template parameter specification of template <class Elem>. Each function identifier is qualified by the template class name and the template argument Array<Elem>.

template <class Elem> Array<Elem>::Array( int sz )
    {size = sz; data = new Elem[size];}

template <class Elem> int Array<Elem>::GetSize()
    { return size; }

This example shows definitions of function members of the String class template.

#include <string.h>
template <unsigned Size> int String<Size>::length( )
    {int len = 0;
      while (len < Size && data[len]!= ’\0’) len++;
      return len;}

template <unsigned Size> String<Size>::String(char *initial)
    {strncpy(data, initial, Size);
      if (length( ) == Size) overflows++;} Static Data Member Definitions

The definition of a template static data member consists of the template parameter specification followed by a variable definition, where the variable identifier is qualified by the class template name and its template actual arguments.

template <unsigned Size> int String<Size>::overflows = 0;

6.2.4 Class Template Use

A template class can be used wherever a type can be used. Specifying a template class consists of providing the values for the template name and arguments. The declaration in the following example creates the variable int_array based upon the Array template. The variable’s class declaration and its set of methods are just like those in the Array template except that Elem is replaced with int. See 6.3 Template Instantiation.

Array<int> int_array(100);

The declaration in this example creates the short_string variable using the String template.

String<8> short_string("hello");

You can use template class member functions as you would any other member function

int x = int_array.GetSize( );
int x = short_string.length( );