I am right now reading this book , C++ templates : Complete guide. At a para i got stuck couldn’t understand the terminology , here is the para:
A fundamental principle is that any template argument must be a
quantity or value that can be determined at compile time. As becomes
clear later, this requirement translates into dramatic benefits for
the run-time costs of template entities. Because template parameters
are eventually substituted by compile-time values, they can themselves
be used to form compile-time expressions. This was exploited in the
ArrayInClass template to size the member array array. The size of an
array must be a so-called constant-expression, and the template
parameter N qualifies as such.We can push this reasoning a little further: Because template
parameters are compile-time entities, they can also be used to create
valid template arguments. Here is an example :
template <typename T>
class Dozen {
public:
ArrayInClass<T,12> contents;
};
Note how in this example the name T is both a template parameter and a
template argument. Thus, a mechanism is available to enable the
construction of more complex templates from simpler ones. Of course,
this is not fundamentally different from the mechanisms that allow us
to assemble types and functions.
I am unable to understand anything. I appreciate any help with simple and understanding words a lot.
Edit:
Arrayinclass:
template <typename T, int N>
class ArrayInClass {
public:
T array[N];
};
There are certain expression in C++ that are required to be known at compile time. For example:
The
30must be a compile-time constant in C++. It could have been:That is fine, because the compiler has all of the information necessary to compute at compile time how big to make the array. However:
This is not a compile-time constant, since the user could call
MyFuncwith any integer value. The compiler does not know how big to make the array, so this is a compiler error.(note: C99 allows for the creation of arrays of arbitrary sizes like this. C++ does not).
Because a template parameter is a compile-time value, it is possible to pass it to other places that require compile-time values:
This is legal C++ because every use of
MyFuncmust specify a compile-time integer: the length of the array. You cannot just callMyFunc()you have to callMyFunc<21>(). And since template arguments must be compile-time determinable values, the user himself cannot provide a value that is not compile-time defined.Because template parameters are always compile-time defined, you can nest templates:
This new template function calls the old one with an array 3 bigger than what it was given.