C ++ 17 separate declaration and definition of a method template instance

I am currently using a separate explicit declaration of an instance of a class template and an explicit definition of an instance of a class template, reducing compilation time and working great.

However, I have some class that is not a template, but only some methods inside the class.

Can you use the same separate declaration and definition mechanism for template methods?

Thank.

template template (working):

a.hpp:

template <class T>
class A {
    void f(T t);
};

// Class explicit template instantiation declaration
extern template class A<int>;
extern template class A<double>;

      

a.cpp:

template <class T>
void A<T>::f(T t) {
}

// Class explicit template instantiation definition
template class A<int>;
template class A<double>;

      

method template (doesn't work):

b.hpp:

class B {
    template <class T>
    void g(T t);
};

// Method explicit template instantiation declaration
extern template method B::g<int>;
extern template method B::g<double>;

      

b.cpp:

template <class T>
void B::f(T t) {
}

// Method explicit template instantiation definition
template method B::g<int>;
template method B::g<double>;

      

+3


source to share


1 answer


Yes.

b.hpp:

extern template void B::g(int);
extern template void B::g(double);

      



b.cpp:

template void B::g(int);
template void B::g(double);

      

+1


source







All Articles