Templates are they are not final code cannot be compiled and linked so templates are lost when compiling is done, and the final object file does not contain any template definitions, only the instantiated methods.
This comes with a consequence we are all used to: templates libraries are header only. They are just like static functions in this regard, each translation unit uses their own copy of the same template function. In C++11 you can prevent this with using extern
templates, but that's far from automatic, and usually it doesn't even worth the time.
The second issue is that unlike using unions, void *
pointers or object inheritance, which are tools that reuse existing code, templates are stupid, and they will generate new code, even if it's the same for any type.
for example:
void * malloc(size_t int);
template<class T> T * talloc(size_t int);
the second function will generate different code for all T types, even though the generated code will be the same.
Of course the compiler will also try and save on the genarated functions. For each class only the functions that are actually used in that translation unit will be generated for any template parameters.
But since the virtual table needs the addresses of all virtual functions if you have a virtual template class, all virtual functions will be generated for any parameter combinations.