[Edited to show split between .cpp and 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>;
The last line causes the following compiler warning in Clang v8.0 warning: explicit template instantiation 'Derived<int>' will emit a vtable in every translation unit [-Wweak-template-vtables]
My understanding is that because Base
has at least one out-of-line virtual method, the vtables for all classes here would be anchored to this translation unit, hence this guidance in the LLVM coding standard. So why is this warning being generated?
See on Godbolt here with specific compiler version I'm using: https://godbolt.org/z/Kus4bq
Every similar question I find on SO is for classes with no out-of-line virtual methods so I have not been able to find an answer.