Gill Bates Gill Bates -4 years ago 71
C++ Question

Why are static members of template classes not unique

Take a look at the following code:

#include <iostream>

template <typename T>
class Foo
{
public:
static T bar;
};

template <typename T> typename T Foo<T>::bar;

int main() {
std::cout << "Foo<int>::bar : " << &Foo<int>::bar << std::endl;
std::cout << "Foo<double>::bar : " << &Foo<double>::bar << std::endl;
return 0;
}


This will print out 2 different addresses. I can understand why in this case,
bar
is of type
T
and thus instantiations of different T's in
Foo<T>
will get you different static members. However, if we change
bar
to a type we already know ( e.g.
static int bar
) this still happens.

Why is this the case? Why just not re-use
bar
for multiple template instantiations? How would I be able to get just 1
bar
object throughout different instantiations?

Answer Source

There is nothing really surprising going on here.

template <typename T>
class Foo
{
    //...
};

Is not a class, it is a template to stamp out classes. That means Foo<A> is a completely different class from Foo<B>. As such all static members are unique to the different instantiated classes — and the fact that class template being same has no relevance in this context, as it is after all a template, the blueprint of the instantiated classes.

If you want all the different kinds of Foo's to share a common state then you can have them inherit from the same base class and put the common information there. Here is a very boiled down example:

struct Foo_Base
{
    static int bar;
};

int Foo_Base::bar = 10;

template<typename T>
struct Foo : Foo_Base {};

int main()
{   
    Foo<int> foo_i;
    Foo<double> foo_d;
    std::cout << foo_i.bar << "\n";
    foo_i.bar += 10;
    std::cout << foo_d.bar;
}

output:

10
20

Live Example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download