clang:没有超出虚拟方法的定义(纯粹的抽象C ++类) [英] clang: no out-of-line virtual method definitions (pure abstract C++ class)
问题描述
我试图使用Clang-3.5编译以下简单的C ++代码:
test.h:
class A
{
public:
A();
virtual〜A()= 0;
};
test.cc:
#includetest.h
A :: A(){;}
A ::〜A(){;}
用于编译此命令(Linux,uname -r:3.16.0-4-amd64) p>
$ clang-3.5 -Weverything -std = c ++ 11 -c test.cc
我得到的错误:
./test.h:1:7:warning:'A'没有超出虚拟方法的定义;其vtable将在每个翻译单元中发出[-Wweak-vtables]
任何提示为什么这是发射一个警告?虚拟析构函数根本不内联。恰恰相反,在test.cc.中提供了一个超出行的定义。
我不认为这个问题是重复的:
什么意思clang的-Wweak-vtables?
as FilipRoséen建议。在我的问题中,我具体指的是纯抽象类(在建议的副本中没有提到)。我知道-Wweak-vtables
如何工作与非抽象类,我很好。在我的例子中,我在实现文件中定义析构函数(它是纯抽象的)。这应该防止Clang发出任何错误,即使使用-Wweak-vtables
。解决方案
因此,代替
class A {
public:
virtual〜A()= 0;
};
我使用
code> class A {
public:
virtual〜A();
};然后在.cpp文件中实现小型析构函数:
$ b
$ bA ::〜A()
{}
b $ b这有效地将vtable固定到.cpp文件,而不是在多个翻译单元(对象)中输出它,并成功避免了-Wweak-vtables警告。
I'm trying to compile the following simple C++ code using Clang-3.5:
test.h:
class A { public: A(); virtual ~A() = 0; };
test.cc:
#include "test.h" A::A() {;} A::~A() {;}
The command that I use for compiling this (Linux, uname -r: 3.16.0-4-amd64):
$clang-3.5 -Weverything -std=c++11 -c test.cc
And the error that I get:
./test.h:1:7: warning: 'A' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Wweak-vtables]
Any hints why this is emitting a warning? The virtual destructor is not inlined at all. Quite the opposite, there's a out-of-line definition provided in test.cc. What am I missing here?
Edit
I don't think that this question is a duplicate of : What is the meaning of clang's -Wweak-vtables? as Filip Roséen suggested. In my question I specifically refer to pure abstract classes (not mentioned in the suggested duplicate). I know how
-Wweak-vtables
works with non-abstract classes and I'm fine with it. In my example I define the destructor (which is pure abstract) in the implementation file. This should prevent Clang from emitting any errors, even with-Wweak-vtables
.解决方案I ended up implementing a trivial virtual destructor, rather than leaving it pure virtual.
So instead of
class A { public: virtual ~A() = 0; };
I use
class A { public: virtual ~A(); };
Then implement the trivial destructor in a .cpp file:
A::~A() {}
This effectively pins the vtable to the .cpp file, instead of outputting it in multiple translation units (objects), and successfully avoids the -Wweak-vtables warning.
这篇关于clang:没有超出虚拟方法的定义(纯粹的抽象C ++类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!