C++17 单独的显式方法模板实例化声明和定义

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

我目前正在使用单独的显式 class 模板实例化声明和显式 class 模板实例化定义,以减少编译时间,并且运行良好。

但是我有一些 class 不是模板,只是 class.

中的一些方法

是否可以对模板方法使用单独声明和定义的相同机制?

谢谢。

class 模板(有效):

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

方法模板(无效):

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

是的。

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