A nontype template argument provided within a template argument list is an expression whose value can be determined at compile time. Such arguments must be constant expressions, addresses of functions or objects with external linkage, or addresses of static class members. Nontype template arguments are normally used to initialize a class or to specify the sizes of class members.
For nontype integral arguments, the instance argument matches the corresponding template argument as long as the instance argument has a value and sign appropriate to the argument type.
For nontype address arguments, the type of the instance argument must be of the form identifier or &identifier, and the type of the instance argument must match the template argument exactly, except that a function name is changed to a pointer to function type before matching.
The resulting values of nontype template arguments within a template argument list form part of the template class's type. If two template class names have the same template name and if their arguments have identical values, they are the same class.
Note: Arguments that contain the < symbol or the
> symbol must be enclosed in parentheses to prevent it from
being parsed as a template argument list delimiter when it is
being used as a relational operator or a nested template
delimiter. For example, the arguments in the following definition
are valid:
myfilebuf<double, (20>10)> x; // valid
The following definition, however, is not valid because the
greater than operator (>) is interpreted as the closing
delimiter of the template argument list:
myfilebuf<double, 20>10> x; // error
If the template arguments do not evaluate identically, the
objects created are of different types:
myfilebuf<double,200> x; // create object x of class // myfilebuf<double,200> myfilebuf<double,200.0> y; // error, 200.0 is a double, // not an int
The instantiation of y fails because the value 200.0 is of
type double, and the template argument is of type int. The
following two objects:
myfilebuf<double, 128> x myfilebuf<double, 512> y
belong to separate template classes, and referencing either of
these objects later with myfilebuf<double> is an error. A c
lass template does not need to have a type argument if it has
nontype arguments. For example, the following template is a valid
class template:
template<int i> class C { public: int k; C() { k = i; } };
This class template can be instantiated by declarations such
as:
class C<100>; class C<200>;
Again, these two declarations refer to distinct classes because the values of their nontype arguments differ.
Class Template Declarations and
Definitions
Explicitly Defined Template Classes
Function Templates
Differences between Class and Function
Templates
Example of Nontype Template
Arguments