Main Page

Previous Page
Next Page

[Page 761 (continued)]

14.5. Nontype Parameters and Default Types for Class Templates

Class template Stack of Section 14.4 used only a type parameter in the template header (line 6). It is also possible to use nontype template parameters or nontype parameters, which can have default arguments and are treated as consts. For example, the template header could be modified to take an int elements parameter as follows:

template< typename T, int elements > // nontype parameter elements

Then, a declaration such as

Stack< double, 100 > mostRecentSalesFigures;

could be used to instantiate (at compile time) a 100-element Stack class-template specialization of double values named mostRecentSalesFigures; this class-template specialization would be of type Stack< double, 100 >. The class header then might contain a private data member with an array declaration such as

T stackHolder[ elements ]; // array to hold Stack contents

In addition, a type parameter can specify a default type. For example,

template< typename T = string > // defaults to type string

might specify that a Stack contains string objects by default. Then, a declaration such as

Stack<> jobDescriptions;

could be used to instantiate a Stack class-template specialization of strings named jobDescriptions; this class-template specialization would be of type Stack< string >. Default type parameters must be the rightmost (trailing) parameters in a template's typeparameter list. When one is instantiating a class with two or more default types, if an omitted type is not the rightmost type parameter in the type-parameter list, then all type parameters to the right of that type also must be omitted.

Performance Tip 14.2

When appropriate, specify the size of a container class (such as an array class or a stack class) at compile time (possibly through a nontype template parameter). This eliminates the executiontime overhead of using new to create the space dynamically.

[Page 762]

Software Engineering Observation 14.3

Specifying the size of a container at compile time avoids the potentially fatal execution-time error if new is unable to obtain the needed memory.

In the exercises, you will be asked to use a nontype parameter to create a template for our class Array developed in Chapter 11. This template will enable Array objects to be instantiated with a specified number of elements of a specified type at compile time, rather than creating space for the Array objects at execution time.

In some cases, it may not be possible to use a particular type with a class template. For example, the Stack template of Fig. 14.2 requires that user-defined types that will be stored in a Stack must provide a default constructor and an assignment operator. If a particular user-defined type will not work with our Stack template or requires customized processing, you can define an explicit specialization of the class template for a particular type. Let's assume we want to create an explicit specialization Stack for Employee objects. To do this, form a new class with the name Stack< Employee > as follows:

class Stack< Employee >
   // body of class definition

Note that the Stack< Employee > explicit specialization is a complete replacement for the Stack class template that is specific to type Employeeit does not use anything from the original class template and can even have different members.

Previous Page
Next Page