c++linkermember-functionsclass-templatetranslation-unit

Definitions of member fuction of class template full specialization in separate TUs


Given a class template, that take too long time to compile. During developement and debugging I want to reduce compilation time by separating the defintions of member functions into separate translation units. Just for the only full specialization (this is also for the sake of compilation time reduction).

Is it possible in C++ to separate definitions of member fuctions of class template full specialization by placing them into separate TUs?

template<> void A<smth>::f() or void A<smth>::f() gives nothing in the attempt. I can't defeat link time error.

Making explicit instantiation declarations (i.e. extern template class...) of the class template visible (or invisible) (coupled with removal of void A<smth>::f()) into TUs, where member functions defined, gives nothing too.


Solution

  • Your syntax for explicit instantiation is wrong (you declare a specialization which is not defined), it should be:

    template<typename T>
    void A<T>::g()
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    
    template void A<int>::g();
    template void A<short>::g();
    

    Demo