infiniteLoop
infiniteLoop

Reputation: 381

C++17 separate explicit method template instantiation declaration and definition

I am currently using separate explicit class template instantiation declaration and explicit class template instantiation definition, to decrease compilation time, and it's working perfectly.

However I have some class that are not templates but only some methods inside the class.

Is it possible to use the same mechanism of separate declaration and definition for template methods ?

Thank you.

class 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 (not working) :

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>;

Upvotes: 3

Views: 308

Answers (1)

Oktalist
Oktalist

Reputation: 14734

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);

Upvotes: 1

Related Questions