When you have a (non-templated) class that contains a static member, like:
class Foo
{
public:
static int x;
};
Then Foo::x
must be defined in one and only one translation unit, or the compiler will complain of multiple definitions. So in somefile.cpp
you'd have to define it:
int Foo::x = 10;
This way, any translation unit that accesses Foo::x
is accessing the same memory address.
But what if Foo
is a class template?
template <class T>
class Foo
{
public:
static int x;
};
Now, Foo<T>::x
can be defined in a header file, by saying:
template <class T>
int Foo<T>::x = 10;
So, if class template Foo
is defined in foo.hpp
, and translation_unit1.cpp
and translation_unit2.cpp
both include foo.hpp
, will the memory address of Foo<T>::x
for some instantiation of template class Foo, (such as Foo<int>::x
for example) be different for each translation unit?