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. |
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:
template<> 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.
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography