A static declaration within a class template declares a static data member for each template class generated from the template. The static declaration can be of template argument type or of any defined type.
Like member function templates , you can explicitly define a
static data member of a template class at file scope for each
type used to instantiate a template class. For example:
template <class T> class key
{
public:
static T x;
};
int key<int>::x;
char key<char>::x;
void main()
{
key<int>::x = 0;
}
You can also define a static data member of a template class
using a template definition at file scope. For example:
template <class T> class key
{
public:
static T x;
};
template <class T> T key<T> ::x; // template definition
void main()
{
key<int>::x = 0;
}
When you instantiate a template class, you must have either an explicit definition or a template definition for each static data member, but not both.
![]()
Example of Static Data Members
in Templates
![]()
Static Members
Explicitly Defined Template Classes
File Scope
Function Templates
Member Function Templates
Explicitly Defined Template Functions