I'm having trouble understanding why my declaring a destructor in my class doesn't delete the implicitly declared move constructor as is specified in this documentation, where it says :
If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true:
- there are no user-declared copy constructors;
- there are no user-declared copy assignment operators;
- there are no user-declared move assignment operators;
- there is no user-declared destructor.
then the compiler will declare a move constructor as a non-explicit inline public member of its class with the signature
T::T(T&&)
.
Note that the the copy constructor, copy assignment and move assignment should all be deleted too if I define my own destructor (hence the rule of 5)
Here's a small code sample to demonstrate my point :
class Test
{
public:
~Test() {}
protected:
int a = 5;
};
void main()
{
Test t1;
Test t2 = std::move(t1); //shouldn't work (note : if we have a copy constructor, will work even if the move constructor doesn't exist)
}
What am I missing? I'm sure it's obvious but I can't seem to find the documentation that explains the aforementioned behavior. I run my code on Visual Studio 2022 with C++20.
I discovered the aforementioned behaviour after having to create a virtual destructor in one of my base class and realizing that I didn't have to redefine all the copy&move constructors/assignments as I thought I'd have to.
Also, it's not 100% clear to me, why defaulting any of the copy/move constructors/assignments specifically with the keyword default
requires in theory to redefine them all (+destructor) ? What's the incentive behind that choice if it's just defaulted ?
Thanks in advance.