-
Notifications
You must be signed in to change notification settings - Fork 947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modifies the description of self-assignment
in the move assignment operator
#4961
base: main
Are you sure you want to change the base?
Conversation
@Mq-b : Thanks for your contribution! The author(s) have been notified to review your proposed change. |
Learn Build status updates of commit eb8b75e: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
#label:"aq-pr-triaged" |
@Mq-b : Thanks for your contribution! The author(s) have been notified to review your proposed change. |
|
||
```cpp | ||
if (this != &other) | ||
{ | ||
} | ||
``` | ||
|
||
There are many other ways, too, such as `copy-and-swap`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We wouldn't say that there are other ways unless we were going to describe them all. Instead, : "Another way is copy-and-swap" without the code ticks. But we've also just complicated the situation by providing an alternative without explaining why it might be desirable. I assume you like it because it does the check to make sure the objects aren't the same for you? If so, let's call that out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In short, I hope that the description is not so "absolute", we should analyze the specific problem.
We don't have to list too many other ways to write it, but in the words Try to say a little that "there are other ways" is still needed.
@@ -125,14 +125,20 @@ The following procedures describe how to write a move constructor and a move ass | |||
} | |||
``` | |||
|
|||
1. In the move assignment operator, add a conditional statement that performs no operation if you try to assign the object to itself. | |||
1. In the move assignment operator, we need to keep the self-assignment safe, and the simple way is to add a judgment directly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the original wording. 'judgement' and 'safe' are uncommon and ambiguous in technical writing. And 'judgement' wouldn't translate well, either. Can you restore the original?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the original wording. 'judgement' and 'safe' are uncommon and ambiguous in technical writing. And 'judgement' wouldn't translate well, either. Can you restore the original?
The original description was too absolute.
What I mean here is to ensure "self-assignment-safe", but the original text does not mention this key term.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to say "[...], no operation should be performed if one tries to assign the object to itself. The easiest way is adding a conditional statement that skips operations when both operands refer to the same object."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple comments.
There are many other ways, too, such as `copy-and-swap`. | ||
|
||
```cpp | ||
shared_ptr(_Right).swap(*this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit weird to mention shared_ptr
here. And shared_ptr(_Right).swap(*this)
is performed in copy assignment, not in move assigment.
In this move-constructors-and-move-assignment-operators-cpp.md
, I think we should say MemoryBlock(std::move(other)).swap(*this)
. BUT we also need to add a swap
function together.
It's curious (or rather old-fashioned) that the whole file mentions neither |
Yes! |
I think the description and handling of securing self-assignment is too simplistic.
Not all cases should use self-assignment checking to ensure self-assignment security, and in many cases it is not necessary.
MSVC STL
std::unique_ptr
、std::shared_ptr
:Foo is inherently self-assignment safe and does not require any additional processing.