Reputation: 83
Upon Lightness Races in Orbit's clarification, I've narrowed my post.
After reading this article: The Rule of Zero,
I came to understand the most, but I still want to solve some unclear issues that I have:
1. Looking at this phrase:
If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if:
X does not have a user-declared copy constructor, and
X does not have a user-declared copy assignment operator,
X does not have a user-declared move assignment operator,
X does not have a user-declared destructor, and
The move constructor would not be implicitly defined as deleted.
Should all 5 statements coexist (share an "and" relation) or only some of them(share an "or" relation)?
2. What "user-declared" copy constructor\copy assignment operator... means?
is declaring it (any one of the list above) in an .h file but not implementing it considered user-declared?
is declaring it (any one of the list above) in an .h file and specify "=deleted" or "=default" considered user-declared?
is declaring it (any one of the list above) in an .h file with empty bracelets ,{}, considered user-declared?
Respectfully,
Etay
Upvotes: 8
Views: 1854
Reputation: 12279
A user declared constructor is a constructor that has been written by the programmer, and not added by the compiler. "User-declared" is the opposite of "implicitly declared" in this case.
Your class will have an implicitly declared default move constructor unless any of that conditions happen in your class. So, it is a "negative and". None of them must happen to get an implicitly declared default move constructor.
In all of your mentioned cases, the constructor is user-declared, even when deleted.
The reason for these rules are for retro-compatibility with pre-C++11. When a user declared a copy constructor, temporaries were send to them too. If you go to a C++11 compiler, and move constructors were indiscriminately implicit, the behaviour would change. Calls that went before to the copy constructor, now go to a phantom move constructor the user could be not aware of.
So, each time the compiler sees a copy constructor or an assignment operator (meaning the class manages its own resources), the behaviour fallbacks to pre-C++11 and move constructors won't be implicitly declared.
Upvotes: 4