Template Arguments

From Appmethod Topics
Jump to: navigation, search

Go Up to Class Templates Overview Index

Multiple arguments are allowed as part of the class template declaration. Template arguments can also represent values in addition to data types:

template<class T, int size = 64> class Buffer { ... };

Both non-type template arguments such as size and type arguments can have default values. The value supplied for a non-type template argument must be a constant expression:

const int N = 128;
int i = 256;
Buffer<int, 2*N> b1;// OK
Buffer<float, i> b2;// Error: i is not constant

Since each instantiation of a template class is indeed a class, it receives its own copy of static members. Similarly, template functions get their own copy of static local variables.