为什么显式模板实例化会在存在外线虚拟时导致 weak-template-vtables 警告?

Why does explicit template instantiation result in weak-template-vtables warning when there are out-of-line virtuals?

[编辑以显示 .cpp 和 hpp 之间的拆分]

// file.hpp
class Base {
 public:
    virtual ~Base(void);
    Base(void);
    Base(const Base&) = default;
};

template<typename T>
class Derived: public Base {
 public:
    Derived(void);
    bool func(void);
};
// file.cpp
#include "file.hpp"

Base::~Base(void) {}
Base::Base(void) {}

template<typename T>
bool Derived<T>::func(void) {return true;}

template<typename T>
Derived<T>::Derived(void) {}

// required to avoid linker errors when using `Derived` elsewhere
template class Derived<int>;

最后一行在 Clang v8.0 中导致以下编译器警告 warning: explicit template instantiation 'Derived<int>' will emit a vtable in every translation unit [-Wweak-template-vtables]

我的理解是,因为Base至少有一个外联虚方法,这里所有类的vtables都会被锚定到这个翻译单元,因此this guidance 在 LLVM 编码标准中。 那么为什么会生成此警告?

在 Godbolt 上查看我正在使用的特定编译器版本:https://godbolt.org/z/Kus4bq

我在 SO 上找到的每个类似问题都是针对 类 的,没有外线虚拟方法,所以我找不到答案。

template class Derived<int>; 是否存在于一个 header-file 中,它再次包含在多个 source-files 中?

在那种情况下,class Derived<int> 的 vtable 和方法将存在于多个 object-files 中。链接器必须弄清楚如何处理这些多个副本。

编译器和链接器应该如何根据 C++ 标准解决这个问题,我不确定。但通常我不在乎,因为副本通常看起来应该是一样的。

但是为了避免这个问题,你应该把 extern template class Derived<int>; 放在头文件中,并且 template class Derived<int>; 正好放在 1 个编译单元中(又名。source-file)

编辑(以反映您将代码拆分为 "file.hpp" 和 "file.cpp"):

我玩了一点 clang-6(我是我的最新版本)

对我来说,警告属于 "If you do X, Y will happen" 类型。但这并不意味着你已经发生了。

在这种情况下,Y 是多个 vtable,只有当您将 template class Derived<int>; 放入多个源文件中时才会发生这种情况,而您不会这样做。

您的来源中的每个 template class Derived<int>; 都会触发警告,因此如果您只看到一个警告,则只有一个 vtable。

但有一种方法可以消除警告:没有显式实例化,依赖编译器隐式实例化 class。

为此,您必须将所有模板定义放在头文件中。所以移动定义:

template<typename T>
bool Derived<T>::func(void) {return true;}

template<typename T>
Derived<T>::Derived(void) {}

到头文件中,并删除 extern template class Derived<int>;template class Derived<int>;

编辑:我认为这不是 Clang 中的错误,而是 Itanium C++ ABI 要求的结果:https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vague-itemplate RecordLayoutBuilder.cpp 中的 Clang 源代码引用了此部分在 computeKeyFunction:

Template instantiations don't have key functions per Itanium C++ ABI 5.2.6. Same behaviour as GCC.

Itanium 规范说 class 模板实例化将存储在目标文件的 COMDAT 部分中。 COMDAT 节用于存储同一对象的多个定义,然后可以统一在link-time。如果模板是按照我在回答中预期的方式编译的,并且有一个关键函数将它锚定到一个特定的翻译单元,那么这将不符合这个 ABI。

我确实认为这个警告没有用,但因为它不是 -Wall 或 -Wextra 的一部分,所以我不太介意。

(以下为原post)

我倾向于认为这是由于 Clang 中的错误所致,报告如下:https://bugs.llvm.org/show_bug.cgi?id=18733

重新post这里的内容以防 link 中断:

Rafael Ávila de Espíndola 2014-02-05 00:00:19 PST

Given

template<typename T>
class foo {
  virtual ~foo() {}
};

extern template class foo<int>;
template class foo<int>;

clang warns:

test.cpp:6:23: warning: explicit template instantiation 'foo<int>' will emit a vtable in every translation unit [-Wweak-template-vtables]
extern template class foo<int>;
                      ^
1 warning generated.

note that the warning points to the explicit template instantiation declaration, but is triggered by the definition. This should probably be checking if the definition is in a header. In a .cpp file there is only one translation unit that sees it.

Comment 1 David Faure 2016-02-13 12:21:27 PST

Yes, this false positive is indeed annoying. Thanks for reporting it. Clang developers: thanks for fixing it :-)

对于任何其他人的意见,我将不胜感激,但我同意错误报告者的观点,即在这种情况下,此警告似乎是虚假的。

尽管错误报告的最后评论称它已修复,但该错误仍以“新”状态列出,所以我不认为它已修复。