C++ Rule of Zero & what is "user-declared" constructor?
Asked Answered
S

1

7

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

Sigismondo answered 4/12, 2016 at 14:31 Comment(8)
This seems to be a lot of questions in one.Magical
Well, I wished to avoid opening separate questions as I thought it's a burden. Besides, All the questions are emphasizing different nuances of the same topic, in my opinion.Sigismondo
No, it's more of a burden when you post multiple questions per question. Please narrow your post.Magical
Ok. Thanks for the clarification. I'll do that.Sigismondo
"User-declared" is used interchangeably with "explicitly declared", as far as I can tell; both in contrast with "implicitly declared".Edy
The actual text in the standard, [class.copy]/9, only has the first four bullet points, and makes it clear all four are AND-ed together. It then separately describes circumstances under which a move constructor declared as defaulted would be defined as deleted.Edy
2. Yes, yes, yes. If the user has written a declaration, the thing being declared is user-declared. What would be the top three reasons to assume otherwise?Torin
n.m. , The reasons which made me think otherwise were for example: 1. if a user declared "copy ctor = default", the compiler itself generates it's implementation so I saw some sort of an edge case here which requires clarification. 2. Only declaring in .h file but not implementing, It seems to me as if the compiler generates the implementation... So, to sum up: If a user wrote the ctor\dtor declaration, even without anything else, it is still considered as user-defined and a move constructor won't be created as default if a user didn't declare it also. Right?Sigismondo
M
4

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.

Momently answered 4/12, 2016 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.