January 2019
Intermediate to advanced
512 pages
14h 5m
English
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 ...