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.3 Template Instantiation

Template instantiation involves generating a concrete class or function (instance) for a particular combination of template arguments. For example, the compiler generates a class for Array<int> and a different class for Array<double>. The new classes are defined by substituting the template arguments for the template parameters in the definition of the template class. In the Array<int> example shown in 6.2 Class Templates, the compiler substitutes int wherever Elem appears.

6.3.1 Implicit Template Instantiation

The use of a template function or template class introduces the need for an instance. If that instance does not already exist, the compiler implicitly instantiates the template for that combination of template arguments.

6.3.2 Explicit Template Instantiation

The compiler implicitly instantiates templates only for those combinations of template arguments that are actually used. This approach may be inappropriate for the construction of libraries that provide templates. C++ provides a facility to explicitly instantiate templates, as seen in the following examples.

6.3.2.1 Explicit Instantiation of Template Functions

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments.

template float twice<float>(float original);

Template arguments may be omitted when the compiler can infer them.

template int twice(int original);

6.3.2.2 Explicit Instantiation of Template Classes

To instantiate a template class explicitly, follow the template keyword by a declaration (not definition) for the class, with the class identifier followed by the template arguments.

template class Array<char>;
template class String<19>;

When you explicitly instantiate a class, all of its members are also instantiated.

6.3.2.3 Explicit Instantiation of Template Class Function Members

To explicitly instantiate a template class function member, follow the template keyword by a declaration (not definition) for the function, with the function identifier qualified by the template class, followed by the template arguments.

template int Array<char>::GetSize();
template int String<19>::length();

6.3.2.4 Explicit Instantiation of Template Class Static Data Members

To explicitly instantiate a template class static data member, follow the template keyword by a declaration (not definition) for the member, with the member identifier qualified by the template class, followed by the template argument.

template int String<19>::overflows;