Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid inlining of explcitly instantiated std::vector<T> code? (Visual Studio C++ 2008)

I want to decrease the size of .obj files in a large project I'm working on (I know that linker removes duplicate definitions, but I want to speed up the build process). One of the reasons for their size is that each class which uses std::vector<double> or std::vector<std::string> ends up compiling the code of this class and placing it in its .obj file. I tried to explicitly instantiate std::vector<double> and use extern template declaration, but it doesn't work -- std::vector in Visual Studio C++ STL has all methods inline. Short of modifying the STL code (which I won't do), is there any way to force the compiler not to inline instantiated methods and to use the externally instantiated version of std::vector<double>?

like image 216
quant_dev Avatar asked Nov 05 '22 07:11

quant_dev


2 Answers

The only thing that comes to mind is writing an inclusion header that defines the std::vector template (but not its members, those only need to be declared) and include that instead of the vector standard header.

Then you can explicitly instantiate std::vector<whatever> in a separate compilation unit and link against that.

To explicitly instantiate the template, don’t use extern template (that won’t work), just use the following:

#include <vector> // The standard header, not your forward-declaration!

template class std::vector<double>;
like image 181
Konrad Rudolph Avatar answered Nov 09 '22 04:11

Konrad Rudolph


What I suggest now I would consider a hack, as it convolute the code to achieve a compiler optimization. Compiler flags should be investigated before you try this.

All in all what compiler puts into the obj files is compiler dependent, but I would suggest to try to inherit an specialization of the vector, and use this specialization. I can imagine that this will make the compiler create an obj file containing the specialization of the vector, allowing all users of the wrapped vector to use this instead.

like image 38
daramarak Avatar answered Nov 09 '22 05:11

daramarak