Separate compilation and template explicit instantiation

后端 未结 2 543
遥遥无期
遥遥无期 2021-01-15 05:03

Summary
This question is about achieving separate compilation of a single template class instantiation in a several different translation units.

<
2条回答
  •  囚心锁ツ
    2021-01-15 05:46

    After another look at the standard, it seems to me that the only reasonable option is to use single explicit template class instantiation combined with explicit member function instantiations of a small number of "difficult" functions.

    This (according to 14.7.2p9) will instantiate the class and all members which have been defined up to this point (which should include everything except "difficult" members). Then those selected members can be explicitly instantiated in other translation units containing their definitions.

    That would make my example look like below (assuming that TA1.cpp contains easy functions and the only "difficult" function in TA is func2)

    file TA1.cpp:

    template 
    void TA::func1() { /* "simple" function definition */ }
    
    template class TA; /* expl. inst. of class */
    

    file TA2.cpp:

    template 
    void TA::func2() { /* "difficult" function definition */ }
    
    template void TA::func2(); /* expl. inst. of member */
    

    This method requires us to write explicit instantiation definition for every "difficult" function, which is tedious but also makes us think twice whether we really want to keep it separately or not.

    Disclaimer

    When that can be useful? Not often. As other people here mentioned, it is not advised to split definitions of classes over several files. In my particular case "difficult" functions contain complicated mathematical operations on instances of non-trivial classes. C++ templates are not famous for fast compilation speeds, but in this case it was unbearable. These functions call each other which sends compiler on long and memory-consuming journey of expanding/inlining overloaded operators/templates/etc to optimize everything it sees, with pretty much zero improvement, but making compilation last for hours. This trick of isolating some functions in separate files speeds up compilation 20 times (and allows to parallelize it as well).

提交回复
热议问题