Duplicate c++ template instantiations

前端 未结 4 788
迷失自我
迷失自我 2020-12-17 16:16

Is it possible for the compiler to duplicate instantiations of a same template across several translation units?

For instance, if you have a.cpp which use a st

4条回答
  •  无人及你
    2020-12-17 16:47

    As sharptooth says, the final binary will only contain one instantiation. But the templates will still be instantiated everytime they are encountered in a compilation unit. If you want some compilation speed, in C++0x we get the extern templates. It works like normal extern variables, in that it has to be specified in at least one compilation unit, but the compiler wont instantiate the template in this unit. See here and this draft (14.7.2 [temp.explicit]) for more infos.

提交回复
热议问题