The following code compiles:
#include <vector>
#include <iostream>
#include <memory>
using namespace std;
class container
{
public:
container(){}
~container(){}
};
class Ship
{
public:
Ship(){}
//Ship(const Ship & other){cout<<"COPY"<<endl;}
//~Ship(){}
std::unique_ptr<container> up;
};
Ship buildShip()
{
Ship tmp;
return tmp;
}
int main(int argc, char *argv[])
{
return 0;
}
But if we include the user defined destructor ~Ship(){}
, the code will only compile if we also include the user defined copy constructor Ship(const Ship & other){cout<<"COPY"<<endl;}
In short:
Compiles:
Ship(){}
//Ship(const Ship & other){cout<<"COPY"<<endl;}
//~Ship(){}
Compiles:
Ship(){}
Ship(const Ship & other){cout<<"COPY"<<endl;}
~Ship(){}
Does NOT Compile:
Ship(){}
//Ship(const Ship & other){cout<<"COPY"<<endl;}
~Ship(){}
Why does the insertion of user defined destructor require an user defined copy constructor and why do we need a copy constructor in the above example at all?
I would expect that there is no copy constructor needed in the example above, as unique_ptr can not even be copied.
The code gets compiled because buildShip()
would use the move constructor automatically generated by the compiler when returning tmp
. Adding user-declared destructor prevents the compiler from auto-generating one. E.g., see this or this questions. And the compiler-generated copy constructor can not be used because of the member up
which is std::unique_ptr
. And copy constuctor of unique_ptr
is explicitly deleted.
So this will compile, because the compiler is explicitly asked to generate the move constructor:
class Ship
{
public:
Ship(){}
Ship(Ship&&) = default;
~Ship(){}
std::unique_ptr<container> up;
};
The idea is that, if the compiler-generated destructor is not good enough for your class, then chances are the copy constructor and copy assignment operator are also not good enough, so the compiler may delete the implicit implementations of those copy operations. Technically, the compiler may still give you implicit copy operations even if you have a user-defined destructor, but that behavior is deprecated in c++11.
See Rule of Three
AFAIK, you still need a copy constructor because buildShip()
returns by value.
(However, it's interesting that you are able to compile the versions that use an implicit copy constructor. You shouldn't be able to do that because of the unique_ptr
member...)