I have objects that holds pointers and shares them with others. Moving them is fine, but copying not. I would like to store them in vectors and maps. The following code works only when A
has no destructor. However I would need a destructor to clean up my pointers.
#include <vector>
struct OnlyMove
{
OnlyMove(const OnlyMove&) = delete;
OnlyMove& operator=(const OnlyMove&) = delete;
OnlyMove(OnlyMove&&) = default;
OnlyMove& operator=(OnlyMove&&) = default;
protected:
OnlyMove() = default;
};
struct A : OnlyMove
{
A(int){}
~A(){} // makes compilation fail
};
int main()
{
std::vector<A> y;
y.emplace_back(1);
}
The error when I have a destructor:
/usr/local/include/c++/8.2.0/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = A; _Args = {A}]': … error: use of deleted function 'OnlyMove::OnlyMove(const OnlyMove&)'
I don't understand why. What role does the destructor play here?