Skip to Content
Hands-On Design Patterns with C++
book

Hands-On Design Patterns with C++

by Fedor G. Pikus
January 2019
Intermediate to advanced
512 pages
14h 5m
English
Packt Publishing
Content preview from Hands-On Design Patterns with C++

Non-type template parameters

Usually, template parameters are types, but C++ also allows for several kinds of non-type parameters. First of all, template parameters can be values of integer or enumeration types:

template <typename T, size_t N> class Array {    public:    T& operator[](size_t i) {        if (i >= N) throw std::out_of_range("Bad index");         return data_[i];    private:    T data_[N];};Array<int, 5> a;        // OKcin >> a[0];Array<int, a[0]> b;     // Error

This is a template with two parameters—the first is a type, but the second is not. It is a value of type size_t that determines the size of the array; the advantage of such a template over a built-in C-style array is that it can do range checking. The C++ standard library has a std::array class template ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Structural Design Patterns in Modern C++

Structural Design Patterns in Modern C++

Umar Lone

Publisher Resources

ISBN: 9781788832564Supplemental Content