#138 Missing template instantiation breaks -O2 build


As discussed in http://bugs.gentoo.org/334797 building rezound with some versions of gcc and -O2 enabled breaks linking due to unresolved references to TStaticPoolAccesser<float, TPoolFile<unsigned long, unsigned long> > TPoolFile<unsigned long, unsigned long>::createPool<float>(string, bool). This can be solved via an explicit instantiation of these TPoolFile accessor functions for sample_t. I will attach a patch.

As one source code comment reads "Some explicit template method instantiations (not sure why some are necessary and some aren't)", here is my opinion on this: You haven't disabled implicit instantiation. So whenever the template definition is visible at the time the template is used, it will be compiled, possibly leading to some redundant work but avoiding linker errors. When the definition isn't visible, the compiler has to defer the problem to the linker, and hope the symbol will be there. This will be the case if some other code did compile the function in question for its own use, but only if it did compile it to a function, not if it did inline it. As compilers might choose to inline code for reasons of optimization, this is very hard to prefict and dependant on the actual compiler flags. So as some other comment wonders why instantiations for float are needed, but not for int16_t: Probably some other code instantiated the int16_t template independent form the configured audio format.

I have the impression that mixing explicit and implicit instantiation is difficult to get right, and even more difficult to check for correctness. You could decide to either use implicit instantiation throughout, making the template definition available at every reference to one of its functions. Or you could switch to explicit instantiation for all your templates, not only the pool file stuff, and use -fno-implicit-templates for all the other files to ensure the instantiations you compile are exhaustive. The former will leave the compiler room for optimization, whereas the latter will reduce compile time somewhat.

See also http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html about details of template instantiation techniques in gcc.


  • Instantiate createPool<sample_t>