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

Preface

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

6.2.3.1 Function Member Definitions

6.2.3.2 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

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

Glossary

Index

6.4 Template Composition

You can use templates in a nested manner. This is particularly useful when defining generic functions over generic data structures, as in the standard C++ library. For example, a template sort function may be declared over a template array class:

template <class Elem> void sort(Array<Elem>);

and defined as:

template <class Elem> void sort(Array<Elem> 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 (store[j-1] > store[j])
                  {Elem temp = store[j];
                    store[j] = store[j-1];
                    store[j-1] = temp;}}

The preceding example defines a sort function over the predeclared Array class template objects. The next example shows the actual use of the sort function.

Array<int> int_array(100);   // construct an array of ints
sort(int_array);             // sort it