18.4 Nontype Parameters

Class template Stack of Section 18.2 used only a type parameter (Fig. 18.2, line 7) in its template declaration. It’s also possible to use nontype template parameters, which can have default arguments and are treated as constants. For example, the C++ standard’s array class template begins with the template declaration:


template <typename T, size_t N>

(Recall that keywords class and typename are interchangeable in template declarations.) So, a declaration such as


array<double, 100> salesFigures;

creates a 100-element array of doubles class-template specialization, then uses it to instantiate the object salesFigures. The array class template encapsulates a built-in array. When you create an array class-template specialization, ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.