There is nothing surprising here.
template <typename T> class Foo {
Not a class, this is a template for stamping classes. This means that Foo<A> is a completely different class from Foo<B> . Thus, all static elements are unique to different instance instances; and the fact that the template template is the same has nothing to do with this context, since it is, after all, a template, a plan of class instances.
If you want all the different types of Foo share a common state, you can inherit them from the same base class and place the general information there. Here is an 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
Living example
source share