The template declaration specifies a set of parameterized classes or functions.
Note For more information, see Template Topics in Visual C++ Programmer’s Guides.
Syntax
template-declaration :
template < template-argument-list > declaration
template-argument-list :
template-argument
template-argument-list , template-argument
template-argument :
type-argument
argument-declaration
type-argument :
class identifier
typename identifier
The declaration declares a function or a class. With function templates, each template-argument must appear at least once in the template-argument-list of the function being declared.
The template-argument-list is a list of arguments used by the template function that specifies which parts of the following code will vary. For example:
template< class T, int i > class MyStack...
In this case the template can receive a type (class T
) and a constant parameter (int I
). The template will use type T
and the constant integer i
upon construction. Within the body of the MyStack
declaration, you must refer to the T
identifier.
The typename keyword can be used in the template-argument-list. The following template declarations are identical:
template< class T1, class T2 > class X...
template< typename T1, typename T2 > class X...
Template arguments of the following form are allowed:
template<typename Type> class allocator {};
template<typename Type,
typename Allocator = allocator<Type> > class stack {
};
stack<int> MyStack;
Visual C++ supports the reuse of template parameters in the template parameter list. For example, the following code is now legal:
class Y {...};
template<class T, T* pT> class X1 {...};
template<class T1, class T2 = T1> class X2 {...};
Y aY;
X1<Y, &aY> x1;
X2<int> x2;
A template declaration itself does not generate code; it specifies a family of classes or functions, one or more of which will be generated when referenced by other code.
Template declarations have global or namespace scope.
Visual C++ performs syntax checking of template definitions. This version of Visual C++ can detect errors that previous versions cannot. The compiler can now detect syntax errors of templates that are defined but never instantiated.
Here is a list of common errors which could compile with the Visual C++ 4.0 compiler, but not the Visual C++ 5.0 or later compiler:
template<class T> class X {
//...
Data m_data;//Error Visual C++ 5.0 or later, Data not defined
};
class Data {...};
void g() { X<int> x1; }
Move the declaration of Data
before the class template X
to fix this problem.template<class T> class X {
//no mf declared here
};
//This definition did not cause an error with Visual
//C++ 4.0, but it will cause an error with Visual
//C++ 5.0 or later
//
template<class T> void X<T>::mf() {...};
template<class T> class X {
friend class Y<T>;//Parsed as Y ‘less-than’
//T ‘greater-than’;
Z<T> mf( );//Parsed as Z ‘less-than’ T
//‘greater-than’;
};
template<class T> class Y {...};
template<class T> class Z {...};
X<int> x;
Y
and Z
before the definition of X
.template<class T> class Y {...};
template<class T> class Z {...};
template<class T> class X {...};