Why is the "error: invalid application" sizeof "for an incomplete type using unique_ptr" fixed by adding an empty destructor?

I am deleting the class STFT. Compiles with this in the header:

class STFT; // pimpl off to prevent point name clash

class Whatever
{
private:
    STFT* stft;

and this is in implementation:

#include "STFT.h"
Whatever::Whatever() : stft(new STFT()) {
// blah blah
}

Whatever::~Whatever() {
    delete stft; // pure evil
}

However, switching to std::unique_ptr<STFT> stft;over the raw pointer in the header and deleting the destructor, I get

error: invalid application 'sizeof' for incomplete STFT type 'static_assert (sizeof (_Tp)> 0, "default_delete cannot delete incomplete type");

But if I just put an empty destructor Whatever::~Whatever(){}, then it compiles fine. It completely stalled me. Please write to me what this meaningless destructor does for me.

+8
2

cppreference std::unique_ptr:

std::unique_ptr T, Pimpl. , T , , , std::unique_ptr. (, std::shared_ptr , , T ).

:

#include <memory>

class STFT; // pimpl off to prevent point name clash

class Whatever
{
    public:
     ~Whatever() ;
    private:
      std::unique_ptr<STFT> stft;
} ;

//class STFT{};

Whatever::~Whatever() {}

int main(){}

, STFT Whatever, stft, , , , STFT .

, , STFT , Whatever::~Whatever(), STFT.

+13

Whatever::~Whatever() = default;

, -, .

+4

Source: https://habr.com/ru/post/1618470/


All Articles