外部模板:声明不声明任何东西

Extern template: declaration does not declare anything

我有模板class

expof.h:

template <class T>
class ExpOf{
...
}

我在我的代码中反复使用它,例如T = double [以及 ExpOf 不应该知道的其他 class ]。 所以我认为一劳永逸地编译它是个好主意[或两次...]

expofdouble.cpp:

#include "expof.h"
template class ExpOf<double>;

并在不同的 header 中声明它,因此当包含 expof.h 时它不会被编译。

expofdouble.h:

extern template ExpOf<double>;

当我编译这个 (clang-800.0.42.1) 时,我收到(许多)警告

expofdouble.h: warning: declaration does not declare anything [-Wmissing-declarations]
extern template ExpOf<double>;
                ^~~~~~~~~~~~~

我得到了想要的行为吗?那为什么要警告?我应该采取不同的做法吗?

expofdouble.h 应包含此行:

extern template class ExpOf<double>;

您的声明省略了 class 关键字,因此它实际上没有声明任何内容。

(请注意,您会收到与 extern int; 等声明相同的警告,这显然对您没有任何用处。)