How to use explicit template instantiation to reduce compilation time?

后端 未结 2 1359
夕颜
夕颜 2021-01-04 06:48

It was suggested to use explicit template instantiation to reduce compilation time. I am wondering how to do it. For example

// a.h
template         


        
2条回答
  •  花落未央
    2021-01-04 07:46

    If you know that your template will be used only for certain types, lets call them T1,T2, you can move implementation to source file, like normal classes.

    //foo.hpp
    template
    struct Foo {
        void f();
    };
    
    //foo.cpp
    template
    void Foo::f() {}
    
    template class Foo;
    template class Foo;
    

提交回复
热议问题