Duplicate c++ template instantiations

前端 未结 4 787
迷失自我
迷失自我 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 17:08

    It is possible, but only if you explicitly instantiate them, but then you will get a linker errors :

    // header.hpp
    template< typename T >
    class A
    {
    };
    
    // source1.cpp
    template class A< int >;
    
    // source2.cpp
    template class A< int >;
    

    If you are not explicitly instantiating templates, then any decent linker will easily eliminate copies.

提交回复
热议问题