Static Variables In Template Classes And Methods

This is the final post in a series on static variables, and in it I’ll talk about the final place we can put them – in template classes or functions.

If you’ve been following along, and you have a good understanding of how template classes work, you can almost certainly deduce how static variables will perform in this situation.

Essentially, because a template class is exactly that – a template – any static variables declared within it are only created if the template class is actually used to create a type.

Let’s quickly run through the basics of what a template class is, then the way that static variables behave will become clearer.

A template class is defined as a generic class that can be used for any particular type. They tend to be containers or collections (for example a vector or map class). They are created and coded in such a way that you can instantiate a class using any of a number of different types: ints, strings, or even other classes that you have created; and you don’t need to rewrite the template class to account for the difference.

Seeing a template class in code is the best way to see what this actually means in practice.

The example program below shows a template class and a static variable that belongs to it.

#include <iostream>

template <class T> class Collection
{
public:
    static int count; //static declaration
    Collection()
    {
        count++;
    }
};

// We initialise the static like this: 
template<class T>
int Collection<T>::count = 0;

int main()
{
    Collection<int> v;
    Collection<int> w;
    Collection<char> x;
    Collection<float> y;
    Collection<int> z;

    std::cout << Collection<int>::count << std::endl;
    std::cout << Collection<char>::count << std::endl;
    std::cout << Collection<float>::count << std::endl;

    return 0;
}

The output of the program above is:

3
1
1

Why?

First, we create five objects from our template class – three ints, one char and one float.

The ints, which are all the same type, share the static variable count. Each time an int template class is created, count is incremented.

The float and the char objects have their own static variable count because they are different types. If you were to create more char and float objects, count would increase for those particular types too.

So there we have it. A very simple template class, with a single static that is created for each different type of template class that is created.

OK, but what about statics in template class methods?

These work in exactly the same way as static variables in regular class methods, but as above, a static is created for each different type of template class that is instantiated.

And statics in template functions?

Again, these work in exactly the same way as static variables in regular functions, but a static is created for each different type of template function that is instantiated.

And that really is about all I have to say on static variables for now.

Oh! Not quite…

I do want to mention the extern keyword, but I’ll save that for another post 🙂


One Comment

  1. vishwanath rana
    Posted 29 October 2015 at 17:48 | Permalink

    Nice post..