c++ - 外部模板 : declaration does not declare anything

标签 c++ templates declaration extern

我有一个模板类

展览.h:

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

我在整个代码中反复使用,例如T = double [和其他类ExpOf应该一无所知]。
所以我认为一次性编译它是个好主意[或两次......]

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

并在不同的头文件中声明它,这样当包含 expof.h 时它不会被编译。

exofdouble.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; 的声明,您将收到相同的警告,这显然对您没有任何用处。)

关于c++ - 外部模板 : declaration does not declare anything,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41900298/

相关文章:

C++两次传递函数指针导致问题

c++ - 为什么这个单独的定义会导致错误?

ios - IOS改名变量UILabel、IBOutlet报错

c++ - 使用 boost multi_index_container 来保留插入顺序

c++ - C++ 数组和指针的 sizeof 结果

C++ 无法为高阶函数派生模板参数

c++ - 如何通过专门化在具有单独声明和定义的模板化类的方法上使用 std::enable_if

c++ - 如何检查 C++ 编译时是否存在运算符的特定重载?

c++ - 在std::exchange中,为什么第二个模板参数默认了?

c++ - 函数声明和定义的返回类型不匹配,编译器可以接受吗?