clang: no out-of-line virtual method definitions (pure abstract C++ class)

后端 未结 4 1968
抹茶落季
抹茶落季 2020-11-29 22:00

I\'m trying to compile the following simple C++ code using Clang-3.5:

test.h:

class A
{
  public:
    A();
    virtual ~A() = 0;
};

4条回答
  •  情深已故
    2020-11-29 22:19

    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.

    As a side effect of explicitly declaring the destructor you no longer get the default copy and move operations. See https://stackoverflow.com/a/29288300/954 for an example where they are redeclared.

提交回复
热议问题