gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:25:03 26 4
gpt4 key购买 nike

我有一个模板类

展览.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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com