I have working code with a template. Similar to the stl::string I am mostly using my template with one parameter across multiple compilation units. To save time I am trying to use extern instantiation. However changing the lines as follows yields an error. What is the right way to do it? (P.S. Compiling on gcc with the c++0x flag)
typedef myTemplate_base<commonType> myTemplate;
extern template class myTemplate_base<commonType>; //using "extern template myTemplate" wont work
I added an extra cpp file with the following to the project.
template class myTemplate_base<commonType>;
The linker comes up with this error messge (giving the line of the first object instantiation (myTemplate someVar;
) in the main file as error source):
undefined reference 'myTemplate_base::~myTemplate_base()'
However this type is in the class with the following definition ~myTemplate() = default;
Edit: If you have a better title in mind please comment, so the right people take a look at this
Edit2: There is on funny thing, the addition of template class myTemplate_base<commonType>
increases the executable size tremendously (+100k on a 450k binary) even though the template is used in the main (to compile I have to comment the extern
part out). This hints that the linker keeps two implementations of a template with the same instantiation/me overlooking something.