I can't understand, why if we define static variable of usual (non-template) class in header, we have linker error, but in case of templates all works fine and moreover we will have single instance of static variable among all translation units:
It's template header (template.h):
// template.h
template<typename T>
class Templ {
public:
static int templStatic;
};
template<typename T> Templ<T>::templStatic = 0;
It's first unit using template (unit1.cpp)
// unit1.cpp
#include "template.h"
int method1() {
return Templ<void>::templStatic++;
}
Second unit here (unit2.cpp):
// unit2.cpp
#include "template.h"
int method2() {
return Templ<void>::templStatic++;
}
And, finally, main.cpp:
// main.cpp
#include <iostream>
int method1();
int method2();
int main(int argc, char** argv) {
std::cout << method1() << std::endl;
std::cout << method2() << std::endl;
}
After compilling, linking and executing this code, we will have following output:
0
1
So, why in case of templates all works fine (and as expected) ? How compiler or linker handle this (we can compile each .cpp file in separated calling of compiler, and then link them with caling to linker, so compiler and linker don't "see" all .cpp files at same time) ?
PS: My compiler: msvcpp 9 (but checked on mingw too)
There is solution, you can create a parent class and put the static variable in it, then make your template class inherit it privately, here's an example:
Output will be:
It's because the definition of the static data member is itself a template. Allowing this is necessary for the same reason you are allowed to have a function template that's not inline multiple times in a program. You need the template to generate the resulting entity (say, a function, or a static data member). If you wouldn't be allowed to put the definition of a static data member, how would you instantiate the following
It's not known what
T
is - the Standard says the definition outside the class template is a template definition, in which the parameters are inherited from its class template owner.I've made some experiment with GCC. In the following, we have one implicit instantiation of
F<float>::value
, and one explicit specialization ofF<char>::value
which has to be defined in a .cpp file to not cause duplicated symbol errors when included multiple times.The second translation unit contains just another implicit instantiation of the same static data member
Here is what we get with GCC - it makes each implicit instantiation into a weak symbols and sticks it into its own section here. Weak symbols will not cause errors when there exist multiple of them at link time. Instead, the linker will choose one instance, and discards the other ones assuming all of them are the same
So as we can see
F<float>::value
is a weak symbol which means the linker can see multiple of these at link time.test
,main
andF<char>::value
are global (non-weak) symbols. Linkingmain1.o
andmain2.o
together, we see in the map output (-Wl,-M
) the followingThis indicates that actually it drops all except one instance.