dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.68k stars 322 forks source link

Virtual Destructors #1304

Open BabaBert opened 6 months ago

BabaBert commented 6 months ago

I saw this mentioned here before, but there seems to be no recommended way of handling virtual destructors. I.e.:

struct SomeStruct {
    static std::unique_ptr<SomeStruct> open(const char* filePath);
    protected:
        virtual ~SomeStruct() {}
}

The error output looks something like this:

...
warning: /usr/include/c++/7/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = SomeStruct]’:
warning: /usr/include/c++/7/bits/unique_ptr.h:263:17:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = SomeStruct; _Dp = std::default_delete<SomeStruct>]’
warning: .../cxxbridge.rs.cc:101:34:   required from here
warning: /usr/include/c++/7/bits/unique_ptr.h:78:2: error: ‘virtual SomeStruct::~SomeStruct()’ is protected within this context
warning:   delete __ptr;
warning:   ^~~~~~
warning: In file included from .../wrapper.hpp:2:0,
warning:                  from .../cxxbridge.rs.cc:1:
warning: something.hpp:40:17: note: declared protected here
warning:          virtual ~SomeStruct();
warning:                  ^          
BabaBert commented 5 months ago

I suppose you have to wrap everything that is not directly supported by cxx?