Example of Nontype Template Arguments

In the following example, a class template is defined that requires a nontype template int argument as well as the type argument:

template<class T, int size> class myfilebuf
{
      T* filepos;
      static int array[size];
public:
      myfilebuf() { /* ... */ }
      ~myfilebuf();
      advance(); // function defined elsewhere in program
};

In this example, the template argument size becomes a part of the template class name. An object of such a template class is created with both the type arguments of the class and the values of any additional template arguments.

An object x, and its corresponding template class with arguments double and size=200, can be created from this template with a value as its second template argument:

myfilebuf<double,200> x;

x can also be created using an arithmetic expression:

myfilebuf<double,10*20> x;

The objects created by these expressions are identical because the template arguments evaluate identically. The value 200 in the first expression could have been represented by an expression whose result at compile time is known to be equal to 200, as shown in the second construction.